{"id":1660,"date":"2020-07-19T16:39:54","date_gmt":"2020-07-19T08:39:54","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=1660"},"modified":"2022-07-07T07:18:07","modified_gmt":"2022-07-06T23:18:07","slug":"spoj-cake3-delicious-cake","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/spoj-cake3-delicious-cake\/","title":{"rendered":"SPOJ CAKE3. Delicious Cake"},"content":{"rendered":"<p><a href=\"https:\/\/vjudge.net\/problem\/SPOJ-CAKE3\">Vjudge<\/a> | <a href=\"https:\/\/www.spoj.com\/problems\/CAKE3\/en\/\">SPOJ<\/a> | <a href=\"https:\/\/t.me\/algorithm_daily_of_minako\/149\">TG<\/a><\/p>\n<p>\u5148\u590d\u4e60\u4e00\u4e0b <a href=\"https:\/\/oi-wiki.org\/dp\/plug\/\">\u63d2\u5934 dp \u5927\u5b57\u5178<\/a>\u3002\u672c\u9898\u663e\u7136\u4f7f\u7528\u67d3\u8272\u6a21\u578b\u3002\u8f6e\u5ed3\u7ebf\u4e0a\u53ea\u6709 m \u4e2a\u4f4d\u7f6e\uff0c\u800c\u4e14\u4e0d\u9700\u8981 roll() \u64cd\u4f5c\uff08\u4e0d\u8fc7\u751f\u6210\u7684\u65b0\u63d2\u5934\uff0c\u8981\u6c42\u524d\u9762\u7684\u533a\u5206\u5f00\u6765\uff0c\u6240\u4ee5\u6211\u4eec\u8fd8\u662f\u7528 m+1 \u6765\u6807\u8bb0\uff09\u3002\u8fd9\u6837\u641e\u5f97\u8bdd\uff0c2\u00d72 \u6211\u4eec\u4f1a\u7b97\u51fa\u7b54\u6848\u7b49\u4e8e 14\uff0c\u4ed4\u7ec6\u89c2\u5bdf\uff0c\u662f\u56e0\u4e3a\u6211\u4eec\u591a\u7b97\u4e86\u4e0b\u9762\u4e24\u79cd\u4e0d\u5408\u6cd5\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u6211\u4eec\u53d1\u73b0\u8fd9\u7c7b\u60c5\u51b5\u90fd\u662f\u56e0\u4e3a\u5728\u524d\u9762\u7684\u8f6c\u79fb\u4e2d\u4ea7\u751f\u4e86\u523b\u75d5\uff0c\u800c\u4f7f\u5f97\u540e\u7eed\u7684\u63d2\u5934\u5408\u5e76\u4e0d\u5408\u6cd5\uff0c\u9700\u8981\u8fdb\u4e00\u6b65\u7f16\u7801\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u7ed9\u6bcf\u4e2a\u8fde\u901a\u5757\u5f3a\u884c\u4e0a\u4e00\u4e2a\u989c\u8272\uff0c\u4e0d\u76f8\u540c\u7684\u989c\u8272\u4e0d\u53ef\u5408\u5e76\u3002\u4f46\u662f\u56e0\u4e3a\u539f\u95ee\u9898\u6ca1\u6709\u989c\u8272\uff0c\u8fd9\u6837\u8ba1\u6570\u8d77\u6765\u4f1a\u6bd4\u8f83\u9ebb\u70e6\uff08\u9700\u8981\u6700\u5c0f\u67d3\u8272\uff0c\u5e76\u4e14\u5fc5\u987b\u4fdd\u8bc1\u76f8\u540c\u989c\u8272\u7684\u8fde\u901a\u5757\u5fc5\u88ab\u5408\u5e76\uff09\u3002<br \/>\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u66b4\u529b\u8bb0\u5f55 C(5,2) \u4e2a\u8fde\u901a\u5757\u4e4b\u95f4\u662f\u5426\u4ea7\u751f\u8fc7\u523b\u75d5\uff0c\u8fd9\u6837\u5408\u5e76\u7684\u65f6\u5019\u76f4\u63a5\u5224\u65ad\u5373\u53ef\u3002<\/p>\n<p>\u523b\u75d5\u7684\u7ef4\u62a4\uff0c\u9996\u5148\u6bcf\u6b21\u72b6\u6001\u8f6c\u79fb\uff0c\u5f53\u524d\u63d2\u5934\u90fd\u9700\u8981\u548c\u76f8\u90bb\u4e24\u4e2a\u63d2\u5934\uff08\u5de6\u63d2\u5934\u548c\u4e0a\u63d2\u5934\uff09\u90fd\u6807\u8bb0\u523b\u75d5\u3002<br \/>\n\u5176\u6b21\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u6700\u5c0f\u8868\u793a\uff0c\u8fde\u901a\u5757\u7684\u8bb0\u53f7\u4f1a\u88ab relable\uff0c\u56e0\u800c\u7f16\u7801\u7684\u65f6\u5019\u4e5f\u9700\u8981\u91cd\u65b0\u6620\u5c04\u3002<br \/>\n\u6700\u540e\u63d2\u5934\u5408\u5e76\u7684\u65f6\u5019\uff0c\u523b\u75d5\u4fe1\u606f\u4e5f\u9700\u8981\u5408\u5e76\u3002<\/p>\n<p>\u6700\u540e\u4e0a\u4e00\u4e0b\u9ad8\u7cbe\u5ea6\u6a21\u677f\u5c31\u597d\u4e86\u3002\u7559\u4e2a <a href=\"https:\/\/oeis.org\/A145835\">OEIS<\/a> \u5730\u5740\u65b9\u4fbf\u5927\u5bb6 debug\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\/*\r\n    This code has been written by MinakoKojima, feel free to ask me question. Blog: https:\/\/www.shuizilong.com\/house\r\n    Template Date: 2015.10.12\r\n    Note: ...\r\n*\/\r\n\r\n#pragma comment(linker, &quot;\/STACK:36777216&quot;)\r\n\/\/#pragma GCC optimize (&quot;O2&quot;)\r\n#define LOCAL\r\n#include &lt;functional&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;fstream&gt;\r\n#include &lt;sstream&gt;\r\n#include &lt;iomanip&gt;\r\n#include &lt;numeric&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;climits&gt;\r\n#include &lt;cassert&gt;\r\n#include &lt;complex&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;bitset&gt;\r\n#include &lt;queue&gt;\r\n#include &lt;stack&gt;\r\n#include &lt;cmath&gt;\r\n#include &lt;ctime&gt;\r\n#include &lt;list&gt;\r\n#include &lt;set&gt;\r\n#include &lt;map&gt;\r\n\r\n\/\/#include &lt;tr1\/unordered_set&gt;\r\n\/\/#include &lt;tr1\/unordered_map&gt;\r\n\/\/#include &lt;array&gt;\r\n\r\nusing namespace std;\r\n\r\n#define REP(i, n) for (int i=0;i&lt;n;++i)\r\n#define FOR(i, a, b) for (int i=a;i&lt;b;++i)\r\n#define DWN(i, b, a) for (int i=b-1;i&gt;=a;--i)\r\n#define REP_1(i, n) for (int i=1;i&lt;=n;++i)\r\n#define FOR_1(i, a, b) for (int i=a;i&lt;=b;++i)\r\n#define DWN_1(i, b, a) for (int i=b;i&gt;=a;--i)\r\n#define REP_C(i, n) for (int n____=n,i=0;i&lt;n____;++i)\r\n#define FOR_C(i, a, b) for (int b____=b,i=a;i&lt;b____;++i)\r\n#define DWN_C(i, b, a) for (int a____=a,i=b-1;i&gt;=a____;--i)\r\n#define REP_N(i, n) for (i=0;i&lt;n;++i)\r\n#define FOR_N(i, a, b) for (i=a;i&lt;b;++i)\r\n#define DWN_N(i, b, a) for (i=b-1;i&gt;=a;--i)\r\n#define REP_1_C(i, n) for (int n____=n,i=1;i&lt;=n____;++i)\r\n#define FOR_1_C(i, a, b) for (int b____=b,i=a;i&lt;=b____;++i)\r\n#define DWN_1_C(i, b, a) for (int a____=a,i=b;i&gt;=a____;--i)\r\n#define REP_1_N(i, n) for (i=1;i&lt;=n;++i)\r\n#define FOR_1_N(i, a, b) for (i=a;i&lt;=b;++i)\r\n#define DWN_1_N(i, b, a) for (i=b;i&gt;=a;--i)\r\n#define REP_C_N(i, n) for (int n____=(i=0,n);i&lt;n____;++i)\r\n#define FOR_C_N(i, a, b) for (int b____=(i=0,b);i&lt;b____;++i)\r\n#define DWN_C_N(i, b, a) for (int a____=(i=b-1,a);i&gt;=a____;--i)\r\n#define REP_1_C_N(i, n) for (int n____=(i=1,n);i&lt;=n____;++i)\r\n#define FOR_1_C_N(i, a, b) for (int b____=(i=a,b);i&lt;=b____;++i)\r\n#define DWN_1_C_N(i, b, a) for (int a____=(i=b,a);i&gt;=a____;--i)\r\n\r\n#define ECH(it, A) for (__typeof((A).begin()) it=(A).begin(); it != (A).end(); ++it)\r\n#define rECH(it, A) for (__typeof((A).rbegin()) it=(A).rbegin(); it != (A).rend(); ++it)\r\n#define REP_S(i, str) for (char*i=str;*i;++i)\r\n#define REP_L(i, hd, suc) for (int i=hd;i;i=suc&#x5B;i])\r\n#define REP_G(i, u) REP_L(i,hd&#x5B;u],suc)\r\n#define REP_SS(x, s) for (int x=s;x;x=(x-1)&amp;s)\r\n#define DO(n) for ( int ____n = n; ____n--&gt;0; )\r\n#define REP_2(i, j, n, m) REP(i, n) REP(j, m)\r\n#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)\r\n#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)\r\n#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)\r\n#define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)\r\n#define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)\r\n\r\n#define ALL(A) A.begin(), A.end()\r\n#define LLA(A) A.rbegin(), A.rend()\r\n#define CPY(A, B) memcpy(A, B, sizeof(A))\r\n#define INS(A, P, B) A.insert(A.begin() + P, B)\r\n#define ERS(A, P) A.erase(A.begin() + P)\r\n#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())\r\n#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())\r\n#define CTN(T, x) (T.find(x) != T.end())\r\n#define SZ(A) int((A).size())\r\n#define PB push_back\r\n#define MP(A, B) make_pair(A, B)\r\n#define PTT pair&lt;T, T&gt;\r\n#define Ts *this\r\n#define rTs return Ts\r\n#define fi first\r\n#define se second\r\n#define re real()\r\n#define im imag()\r\n\r\n#define Rush for(int ____T=RD(); ____T--;)\r\n#define Display(A, n, m) {                      \\\r\n  REP(i, n){\t\t                            \\\r\n        REP(j, m-1) cout &lt;&lt; A&#x5B;i]&#x5B;j] &lt;&lt; &quot; &quot;;     \\\r\n        cout &lt;&lt; A&#x5B;i]&#x5B;m-1] &lt;&lt; endl;\t\t        \\\r\n\t}\t\t\t\t\t\t                    \\\r\n}\r\n#define Display_1(A, n, m) {                    \\\r\n\tREP_1(i, n){\t\t                        \\\r\n        REP_1(j, m-1) cout &lt;&lt; A&#x5B;i]&#x5B;j] &lt;&lt; &quot; &quot;;   \\\r\n        cout &lt;&lt; A&#x5B;i]&#x5B;m] &lt;&lt; endl;\t\t        \\\r\n\t}\t\t\t\t\t\t                    \\\r\n}\r\n\r\ntypedef long long LL;\r\n\/\/typedef long double DB;\r\ntypedef double DB;\r\ntypedef unsigned uint;\r\ntypedef unsigned long long uLL;\r\n\r\ntypedef vector&lt;int&gt; VI;\r\ntypedef vector&lt;char&gt; VC;\r\ntypedef vector&lt;string&gt; VS;\r\ntypedef vector&lt;LL&gt; VL;\r\ntypedef vector&lt;DB&gt; VF;\r\ntypedef set&lt;int&gt; SI;\r\ntypedef set&lt;string&gt; SS;\r\ntypedef map&lt;int, int&gt; MII;\r\ntypedef map&lt;string, int&gt; MSI;\r\ntypedef pair&lt;int, int&gt; PII;\r\ntypedef pair&lt;LL, LL&gt; PLL;\r\ntypedef vector&lt;PII&gt; VII;\r\ntypedef vector&lt;VI&gt; VVI;\r\ntypedef vector&lt;VII&gt; VVII;\r\n\r\ntemplate&lt;class T&gt; inline T&amp; RD(T &amp;);\r\ntemplate&lt;class T&gt; inline void OT(const T &amp;);\r\n\/\/inline int RD(){int x; return RD(x);}\r\ninline LL RD(){LL x; return RD(x);}\r\ninline DB&amp; RF(DB &amp;);\r\ninline DB RF(){DB x; return RF(x);}\r\ninline char* RS(char *s);\r\ninline char&amp; RC(char &amp;c);\r\ninline char RC();\r\ninline char&amp; RC(char &amp;c){scanf(&quot; %c&quot;, &amp;c); return c;}\r\ninline char RC(){char c; return RC(c);}\r\n\/\/inline char&amp; RC(char &amp;c){c = getchar(); return c;}\r\n\/\/inline char RC(){return getchar();}\r\n\r\ntemplate&lt;class T&gt; inline T&amp; RDD(T &amp;);\r\ninline LL RDD(){LL x; return RDD(x);}\r\n\r\ntemplate&lt;class T0, class T1&gt; inline T0&amp; RD(T0 &amp;x0, T1 &amp;x1){RD(x0), RD(x1); return x0;}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline T0&amp; RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2){RD(x0), RD(x1), RD(x2); return x0;}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline T0&amp; RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline T0&amp; RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline T0&amp; RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4, T5 &amp;x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline T0&amp; RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2, T3 &amp;x3, T4 &amp;x4, T5 &amp;x5, T6 &amp;x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}\r\ntemplate&lt;class T0, class T1&gt; inline void OT(const T0 &amp;x0, const T1 &amp;x1){OT(x0), OT(x1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void OT(const T0 &amp;x0, const T1 &amp;x1, const T2 &amp;x2){OT(x0), OT(x1), OT(x2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void OT(const T0 &amp;x0, const T1 &amp;x1, const T2 &amp;x2, const T3 &amp;x3){OT(x0), OT(x1), OT(x2), OT(x3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void OT(const T0 &amp;x0, const T1 &amp;x1, const T2 &amp;x2, const T3 &amp;x3, const T4 &amp;x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void OT(const T0 &amp;x0, const T1 &amp;x1, const T2 &amp;x2, const T3 &amp;x3, const T4 &amp;x4, const T5 &amp;x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void OT(const T0 &amp;x0, const T1 &amp;x1, const T2 &amp;x2, const T3 &amp;x3, const T4 &amp;x4, const T5 &amp;x5, const T6 &amp;x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}\r\ninline char&amp; RC(char &amp;a, char &amp;b){RC(a), RC(b); return a;}\r\ninline char&amp; RC(char &amp;a, char &amp;b, char &amp;c){RC(a), RC(b), RC(c); return a;}\r\ninline char&amp; RC(char &amp;a, char &amp;b, char &amp;c, char &amp;d){RC(a), RC(b), RC(c), RC(d); return a;}\r\ninline char&amp; RC(char &amp;a, char &amp;b, char &amp;c, char &amp;d, char &amp;e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}\r\ninline char&amp; RC(char &amp;a, char &amp;b, char &amp;c, char &amp;d, char &amp;e, char &amp;f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}\r\ninline char&amp; RC(char &amp;a, char &amp;b, char &amp;c, char &amp;d, char &amp;e, char &amp;f, char &amp;g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}\r\ninline DB&amp; RF(DB &amp;a, DB &amp;b){RF(a), RF(b); return a;}\r\ninline DB&amp; RF(DB &amp;a, DB &amp;b, DB &amp;c){RF(a), RF(b), RF(c); return a;}\r\ninline DB&amp; RF(DB &amp;a, DB &amp;b, DB &amp;c, DB &amp;d){RF(a), RF(b), RF(c), RF(d); return a;}\r\ninline DB&amp; RF(DB &amp;a, DB &amp;b, DB &amp;c, DB &amp;d, DB &amp;e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}\r\ninline DB&amp; RF(DB &amp;a, DB &amp;b, DB &amp;c, DB &amp;d, DB &amp;e, DB &amp;f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}\r\ninline DB&amp; RF(DB &amp;a, DB &amp;b, DB &amp;c, DB &amp;d, DB &amp;e, DB &amp;f, DB &amp;g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}\r\ninline void RS(char *s1, char *s2){RS(s1), RS(s2);}\r\ninline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}\r\ntemplate&lt;class T0,class T1&gt;inline T0&amp; RDD(T0&amp;a, T1&amp;b){RDD(a),RDD(b); return a;}\r\ntemplate&lt;class T0,class T1,class T2&gt;inline T1&amp; RDD(T0&amp;a, T1&amp;b, T2&amp;c){RDD(a),RDD(b),RDD(c); return a;}\r\n\r\ntemplate&lt;class T&gt; inline void RST(T &amp;A){memset(A, 0, sizeof(A));}\r\ntemplate&lt;class T&gt; inline void FLC(T &amp;A, int x){memset(A, x, sizeof(A));}\r\ntemplate&lt;class T&gt; inline void CLR(T &amp;A){A.clear();}\r\n\r\ntemplate&lt;class T0, class T1&gt; inline void RST(T0 &amp;A0, T1 &amp;A1){RST(A0), RST(A1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2){RST(A0), RST(A1), RST(A2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3){RST(A0), RST(A1), RST(A2), RST(A3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5, T6 &amp;A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}\r\ntemplate&lt;class T0, class T1&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, int x){FLC(A0, x), FLC(A1, x);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void FLC(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5, T6 &amp;A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}\r\ntemplate&lt;class T&gt; inline void CLR(priority_queue&lt;T&gt; &amp;Q){while (!Q.empty()) Q.pop();}\r\ntemplate&lt;class T&gt; inline void CLR(stack&lt;T&gt; &amp;S){while (!S.empty()) S.pop();}\r\ntemplate&lt;class T&gt; inline void CLR(queue&lt;T&gt; &amp;Q){while (!Q.empty()) Q.pop();}\r\n\r\ntemplate&lt;class T0, class T1&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1){CLR(A0), CLR(A1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2){CLR(A0), CLR(A1), CLR(A2);}\r\ntemplate&lt;class T0, class T1, class T2, class T3&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}\r\ntemplate&lt;class T0, class T1, class T2, class T3, class T4, class T5, class T6&gt; inline void CLR(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3, T4 &amp;A4, T5 &amp;A5, T6 &amp;A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}\r\ntemplate&lt;class T&gt; inline void CLR(T &amp;A, int n){REP(i, n) CLR(A&#x5B;i]);}\r\n\r\ntemplate&lt;class T&gt; inline bool EPT(T &amp;a){return a.empty();}\r\ntemplate&lt;class T&gt; inline T&amp; SRT(T &amp;A){sort(ALL(A)); return A;}\r\ntemplate&lt;class T, class C&gt; inline T&amp; SRT(T &amp;A, C cmp){sort(ALL(A), cmp); return A;}\r\ntemplate&lt;class T&gt; inline T&amp; RVS(T &amp;A){reverse(ALL(A)); return A;}\r\ntemplate&lt;class T&gt; inline T&amp; UNQQ(T &amp;A){A.resize(unique(ALL(A))-A.begin());return A;}\r\ntemplate&lt;class T&gt; inline T&amp; UNQ(T &amp;A){SRT(A);return UNQQ(A);}\r\ntemplate&lt;class T, class C&gt; inline T&amp; UNQ(T &amp;A, C cmp){SRT(A, cmp);return UNQQ(A);}\r\n\r\n\r\n\/\/}\r\n\r\n\/** Constant List .. **\/ \/\/{\r\n\r\nconst int MOD = int(1e9) + 7;\r\nconst int INF = 0x3f3f3f3f;\r\nconst LL INFF = 0x3f3f3f3f3f3f3f3fLL;\r\nconst DB EPS = 1e-9;\r\nconst DB OO = 1e20;\r\nconst DB PI = acos(-1.0); \/\/M_PI;\r\n\r\nconst int dx&#x5B;] = {-1, 1, 0, 0};\r\nconst int dy&#x5B;] = {0, 0, 1, -1};\r\n\r\n\/\/}\r\n\r\n\/** Add On .. **\/ \/\/{\r\n\/\/ &lt;&lt;= '0. Nichi Joo ., \/\/{\r\n\r\ntemplate&lt;class T&gt; inline bool checkMin(T &amp;a,const T b){return b &lt; a ? a = b, 1 : 0;}\r\ntemplate&lt;class T&gt; inline bool checkMax(T &amp;a,const T b){return a &lt; b ? a = b, 1 : 0;}\r\ntemplate &lt;class T, class C&gt; inline bool checkUpd(T&amp; a, const T b, C c){return c(b,a) ? a = b, 1 : 0;}\r\ntemplate&lt;class T&gt; inline T min(T a, T b, T c){return min(min(a, b), c);}\r\ntemplate&lt;class T&gt; inline T max(T a, T b, T c){return max(max(a, b), c);}\r\ntemplate&lt;class T&gt; inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}\r\ntemplate&lt;class T&gt; inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}\r\ntemplate&lt;class T&gt; inline T min(T a, T b, T c, T d, T e){return min(min(min(a,b),min(c,d)),e);}\r\ntemplate&lt;class T&gt; inline T max(T a, T b, T c, T d, T e){return max(max(max(a,b),max(c,d)),e);}\r\ntemplate&lt;class T&gt; inline T sqr(T a){return a*a;}\r\ntemplate&lt;class T&gt; inline T cub(T a){return a*a*a;}\r\ntemplate&lt;class T&gt; inline T ceil(T x, T y){return (x - 1) \/ y + 1;}\r\ntemplate&lt;class T&gt; T abs(T x){return x&gt;0?x:-x;}\r\ninline int sgn(DB x){return x &lt; -EPS ? -1 : x &gt; EPS;}\r\ninline int sgn(DB x, DB y){return sgn(x - y);}\r\n\r\ninline DB cos(DB a, DB b, DB c){return (sqr(a)+sqr(b)-sqr(c))\/(2*a*b);}\r\ninline DB cot(DB x){return 1.\/tan(x);};\r\ninline DB sec(DB x){return 1.\/cos(x);};\r\ninline DB csc(DB x){return 1.\/sin(x);};\r\n\r\n\/\/}\r\n\r\n\/\/}\r\n\r\n\r\n\r\n\/** I\/O Accelerator Interface .. **\/ \/\/{\r\n#define g (c=getchar())\r\n#define d isdigit(g)\r\n#define p x=x*10+c-'0'\r\n#define n x=x*10+'0'-c\r\n#define pp l\/=10,p\r\n#define nn l\/=10,n\r\ntemplate&lt;class T&gt; inline T&amp; RD(T &amp;x){\r\n    char c;while(!d);x=c-'0';while(d)p;\r\n    return x;\r\n}\r\ntemplate&lt;class T&gt; inline T&amp; RDD(T &amp;x){\r\n    char c;while(g,c!='-'&amp;&amp;!isdigit(c));\r\n    if (c=='-'){x='0'-g;while(d)n;}\r\n    else{x=c-'0';while(d)p;}\r\n    return x;\r\n}\r\ninline DB&amp; RF(DB &amp;x){\r\n    \/\/scanf(&quot;%lf&quot;, &amp;x);\r\n    char c;while(g,c!='-'&amp;&amp;c!='.'&amp;&amp;!isdigit(c));\r\n    if(c=='-')if(g=='.'){x=0;DB l=1;while(d)nn;x*=l;}\r\n        else{x='0'-c;while(d)n;if(c=='.'){DB l=1;while(d)nn;x*=l;}}\r\n    else if(c=='.'){x=0;DB l=1;while(d)pp;x*=l;}\r\n        else{x=c-'0';while(d)p;if(c=='.'){DB l=1;while(d)pp;x*=l;}}\r\n    return x;\r\n}\r\n#undef nn\r\n#undef pp\r\n#undef n\r\n#undef p\r\n#undef d\r\n#undef g\r\ninline char* RS(char *s){\r\n    \/\/gets(s);\r\n    scanf(&quot;%s&quot;, s);\r\n    return s;\r\n}\r\n\r\nLL last_ans; int Case; template&lt;class T&gt; inline void OT(const T &amp;x){\r\n    \/\/printf(&quot;Case #%d: &quot;, ++Case);\r\n    \/\/printf(&quot;%lld\\n&quot;, x);\r\n    \/\/printf(&quot;%I64d\\n&quot;, x);\r\n    \/\/printf(&quot;%.9f\\n&quot;, x);\r\n    \/\/printf(&quot;%d\\n&quot;, x);\r\n    cout &lt;&lt; x &lt;&lt; endl;\r\n    \/\/last_ans = x;\r\n}\r\n\r\n\r\n\/\/}\/* .................................................................................................................................. *\/\r\n\r\n\r\n\/\/ &lt;&lt;= 'Bignum ., \/\/{\r\nnamespace BIGNUM{\r\n\r\nconst int __base = 1e8;\r\nconst int P10&#x5B;] = {1, 10, int(1e2), int(1e3), int(1e4), int(1e5), int(1e6), int(1e7), int(1e8), int(1e9)};\r\nconst int MAX_BUF_SIZE = 109;\r\nchar __buf&#x5B;MAX_BUF_SIZE];\r\n\r\nclass bignum{\r\n          friend istream&amp; operator&gt;&gt;(istream&amp;, bignum&amp;);\r\n          friend ostream&amp; operator&lt;&lt;(ostream&amp;, const bignum&amp;);\r\n          friend bignum operator +(const bignum&amp;, const bignum&amp;);\r\n          friend bignum operator +(const bignum&amp;, const int&amp;);\r\n          friend bool operator ==(const bignum&amp;, const bignum&amp;);\r\n          friend bool operator !=(const bignum&amp;, const bignum&amp;);\r\n          friend bool operator &lt;(const bignum&amp;, const bignum&amp;);\r\n          friend bool operator &gt;(const bignum&amp;, const bignum&amp;);\r\n          friend bool operator &lt;=(const bignum&amp;, const bignum&amp;);\r\n          friend bool operator &gt;=(const bignum&amp;, const bignum&amp;);\r\n          friend bool operator ==(const bignum&amp;, const int&amp;);\r\n          friend bool operator !=(const bignum&amp;, const int&amp;);\r\n          friend bool operator &lt;(const bignum&amp;, const int&amp;);\r\n          friend bool operator &gt;(const bignum&amp;, const int&amp;);\r\n          friend bool operator &lt;=(const bignum&amp;, const int&amp;);\r\n          friend bool operator &gt;=(const bignum&amp;, const int&amp;);\r\n          friend int do_comp(const bignum&amp;, const int&amp;);\r\n          friend int do_comp(const bignum&amp;, const bignum&amp;);\r\n          friend void divide(const bignum&amp;, const bignum&amp;, bignum&amp;, bignum&amp;);\r\n          friend bignum pow(bignum, int);\r\n          friend bignum pow(int, int);\r\n    public:\r\n        inline bignum(){};\r\n        inline bignum(int s){\r\n            while (s) data.PB(s%__base), s\/=__base;\r\n            if (data.empty()) data.PB(0);\r\n        }\r\n\r\n        inline bignum(long long s){\r\n            while (s) data.PB(int(s%__base)), s\/=__base;\r\n            if (data.empty()) data.PB(0);\r\n        }\r\n\r\n        inline bignum(string s){\r\n            int t, i; CLR(data);\r\n            for (i=int(s.size())-8;i&gt;0;i-=8){\r\n                istringstream(s.substr(i, 8)) &gt;&gt; t;\r\n                data.PB(t);\r\n            }\r\n            istringstream(s.substr(0, i+8)) &gt;&gt; t;\r\n            data.PB(t);\r\n        }\r\n\r\n        void input(){\r\n            CLR(data); RS(__buf); int t = 0, c = 0;\r\n            DWN(i, strlen(__buf), 0){\r\n                t += P10&#x5B;c] * (int(__buf&#x5B;i]) - '0'), ++c;\r\n                if (c == 8) data.PB(t), c = t = 0;\r\n            }\r\n            if (c) data.PB(t);\r\n        }\r\n\r\n        bignum&amp;operator=(int);\r\n        bignum&amp;operator=(const string&amp;);\r\n        bignum&amp;operator=(const bignum&amp;);\r\n        bignum&amp;operator +=(const bignum&amp;);\r\n        bignum&amp;operator -=(const bignum&amp;);\r\n        bignum&amp;operator *=(const bignum&amp;);\r\n        bignum&amp;operator \/=(const bignum&amp;);\r\n        bignum&amp;operator %=(const bignum&amp;);\r\n        bignum&amp;operator +=(const int&amp;);\r\n        bignum&amp;operator -=(const int&amp;);\r\n        bignum&amp;operator *=(const int&amp;);\r\n        bignum&amp;operator \/=(const int&amp;);\r\n        bignum&amp;operator %=(const int&amp;);\r\n        bool undefined();\r\n        int do_try(const int&amp;);\r\n        int do_try(const bignum&amp;);\r\n        void do_trim();\r\n        list&lt;int&gt; data;\r\n\r\n    int size(){\r\n        list&lt;int&gt;::iterator it; int res = 0;\r\n        for (it=data.begin(); it!=data.end();it++)\r\n            res += 8;\r\n        it--;\r\n        if (*it &gt;= 10000) {\r\n            if ( (*it) &gt;= 1000000) {if (*it &gt;=10000000) ; else res--;}\r\n            else {if ((*it) &gt;= 100000) res-=2; else res-=3;}\r\n        }\r\n        else\r\n            if ( (*it) &gt;= 100) {if (*it &gt;=1000) res-=4; else res-=5;}\r\n            else {if ((*it) &gt;= 10) res-=6; else res-=7;}\r\n\r\n        return res;\r\n    }\r\n\r\n    void do_reserve(int a){\r\n        if (a &lt;= 0) return;\r\n        list&lt;int&gt;::iterator it;\r\n        for (it=data.begin(); it!=data.end() &amp;&amp; a&gt;0; it++) a-=8;\r\n        if (it == data.end() &amp;&amp; a&gt;=0) return;\r\n        a+=8, it--; int f = 1;\r\n        for (int i=0;i&lt;a;i++) f *= 10; (*it) %= f;\r\n        data.erase(++it, data.end());\r\n        do_trim();\r\n    }\r\n};\r\n\r\ninline bignum&amp;bignum::operator =(const bignum&amp;a){\r\n    data.clear();\r\n    for (list&lt;int&gt;::const_iterator i=a.data.begin();i!=a.data.end();i++){\r\n        data.PB(*i);\r\n    }\r\n    return*this;\r\n}\r\ninline bignum&amp;bignum::operator =(const string&amp;a){\r\n    return*this=bignum(a);\r\n}\r\ninline bignum&amp;bignum::operator =(int a){\r\n    return*this=bignum(a);\r\n}\r\n\r\ninline istream&amp; operator&gt;&gt;(istream&amp; input, bignum&amp; a){\r\n    string s; int t, i; input &gt;&gt; s; a.data.clear();\r\n    for (i=int(s.size())-8;i&gt;0;i-=8){\r\n        istringstream(s.substr(i, 8)) &gt;&gt; t;\r\n        a.data.PB(t);\r\n    }\r\n    istringstream(s.substr(0, i+8)) &gt;&gt; t;\r\n    a.data.PB(t);\r\n    return input;\r\n}\r\n\r\ninline ostream&amp; operator&lt;&lt;(ostream&amp; output, const bignum&amp; a){\r\n    list&lt;int&gt;::const_reverse_iterator i=a.data.rbegin();\r\n    output &lt;&lt; *i;\r\n    for (i++;i!=a.data.rend();i++){\r\n        if (*i &gt;= 10000) {\r\n            if (*i &gt;= 1000000) {if (*i&gt;=10000000) cout &lt;&lt; *i; else cout &lt;&lt; 0 &lt;&lt; *i;}\r\n            else {if (*i&gt;=100000) cout &lt;&lt; &quot;00&quot; &lt;&lt; *i; else cout &lt;&lt; &quot;000&quot; &lt;&lt; *i;}\r\n        }\r\n        else {\r\n            if (*i &gt;= 100) {if (*i&gt;=1000)  cout &lt;&lt; &quot;0000&quot; &lt;&lt; *i; else cout &lt;&lt; &quot;00000&quot; &lt;&lt; *i;}\r\n            else { if (*i&gt;=10) cout &lt;&lt; &quot;000000&quot; &lt;&lt; *i; else cout &lt;&lt; &quot;0000000&quot; &lt;&lt; *i;}\r\n        }\r\n    }\r\n    return output;\r\n}\r\n\r\ninline bool bignum::undefined(){\r\n    return data.empty();\r\n}\r\n\r\ninline int do_comp(const bignum&amp; a, const bignum&amp; b){\r\n    if (a.data.size()&lt;b.data.size()) return -1; if (a.data.size()&gt;b.data.size()) return 1;\r\n    list&lt;int&gt;::const_reverse_iterator i; list&lt;int&gt;::const_reverse_iterator j;\r\n    for (i=a.data.rbegin(),j=b.data.rbegin(); j!=b.data.rend(); i++,j++){\r\n        if (*i&lt;*j) return -1;              \/\/!!!!\r\n        if (*i&gt;*j) return 1;\r\n    }\r\n    return 0;\r\n}\r\ninline int do_comp(const bignum&amp; a, const int&amp; b){\r\n    return do_comp(a, bignum(b));\r\n}\r\n\r\ninline bool operator ==(const bignum&amp; a, const bignum&amp; b){\r\n    return do_comp(a, b) == 0;\r\n}\r\ninline bool operator !=(const bignum&amp; a, const bignum&amp; b){\r\n    return do_comp(a, b) != 0;\r\n}\r\ninline bool operator &lt;(const bignum&amp; a, const bignum&amp; b){\r\n    return do_comp(a, b) == -1;\r\n}\r\ninline bool operator &gt;(const bignum&amp; a, const bignum&amp; b){\r\n    return do_comp(a, b) == 1;\r\n}\r\ninline bool operator &lt;=(const bignum&amp; a, const bignum&amp; b){\r\n    return do_comp(a, b) != 1;\r\n}\r\ninline bool operator &gt;=(const bignum&amp; a, const bignum&amp; b){\r\n    return do_comp(a, b) != -1;\r\n}\r\n\r\ninline bool operator ==(const bignum&amp; a, const int&amp; b){\r\n    return do_comp(a, b) == 0;\r\n}\r\ninline bool operator !=(const bignum&amp; a, const int&amp; b){\r\n    return do_comp(a, b) != 0;\r\n}\r\ninline bool operator &lt;(const bignum&amp; a, const int&amp; b){\r\n    return do_comp(a, b) == -1;\r\n}\r\ninline bool operator &gt;(const bignum&amp; a, const int&amp; b){\r\n    return do_comp(a, b) == 1;\r\n}\r\ninline bool operator &lt;=(const bignum&amp; a, const int&amp; b){\r\n    return do_comp(a, b) != 1;\r\n}\r\ninline bool operator &gt;=(const bignum&amp; a, const int&amp; b){\r\n    return do_comp(a, b) != -1;\r\n}\r\n\r\ninline void bignum::do_trim(){\r\n    while (data.size()&gt;1&amp;&amp;data.back()==0) data.pop_back();\r\n}\r\n\r\ninline bignum&amp; bignum::operator +=(const bignum&amp; a){\r\n    list&lt;int&gt;::iterator i; list&lt;int&gt;::const_iterator j; int t = 0;\r\n    for (i=data.begin(),j=a.data.begin(); i!=data.end()&amp;&amp;j!=a.data.end(); i++,j++){\r\n        *i+=*j+t; t=*i\/__base; *i%=__base;\r\n    }\r\n    while (i!=data.end()) {*i+=t; t=*i\/__base; *i%=__base; i++;}\r\n    while (j!=a.data.end()) {data.PB(t+*j); t=data.back()\/__base; data.back()%=__base; j++;}\r\n    if (t!=0) data.PB(t);\r\n    return *this;\r\n}\r\n\r\ninline bignum&amp; bignum::operator +=(const int&amp; a){\r\n    return (*this)+=bignum(a);\r\n}\r\n\r\ninline bignum operator +(const bignum&amp; a, const bignum&amp; b){\r\n    list&lt;int&gt;::const_iterator i, j; bignum c; int t = 0;\r\n    for (i=a.data.begin(),j=b.data.begin(); i!=a.data.end()&amp;&amp;j!=b.data.end(); i++,j++){\r\n        c.data.PB(t+*i+*j);\r\n        t=c.data.back()\/__base; c.data.back()%=__base;\r\n    }\r\n    while (i!=a.data.end()) {c.data.PB(t+*i); t=c.data.back()\/__base; c.data.back()%=__base; i++;}\r\n    while (j!=b.data.end()) {c.data.PB(t+*j); t=c.data.back()\/__base; c.data.back()%=__base; j++;}\r\n    if (t!=0) c.data.PB(t);\r\n    return c;\r\n}\r\n\r\ninline bignum operator +(const bignum&amp; a, const int&amp; b){\r\n    return a+bignum(b);\r\n}\r\n\r\n} using namespace BIGNUM;\r\n\/\/}\r\n\r\ntypedef int T_state;\r\ntypedef bignum T_key;\r\nconst int N = 8;\r\nint n, m;\r\n\r\n\/\/ 5*3 = 15\r\n\/\/ 4321 = 10\r\n\r\nconst int Offset = 3, Mask = (1 &lt;&lt; Offset) - 1;\r\nbool ok&#x5B;N]&#x5B;N], okk&#x5B;N]&#x5B;N], _ok&#x5B;N];\r\n\r\nint b&#x5B;N+2], bb&#x5B;N+3];\r\nT_state encode() {\r\n    T_state s = 0; memset(bb, -1, sizeof(bb)); int bn = 1; bb&#x5B;0] = 0;\r\n#define bi bb&#x5B;b&#x5B;i]]\r\n#define bj bb&#x5B;b&#x5B;j]]\r\n    DWN(i, m, 0) {\r\n        if (!~bi) bi = bn++;\r\n    }\r\n\r\n    memset(okk, 1, sizeof(okk));\r\n    \/\/cout &lt;&lt; &quot;b: &quot;; FOR(i, 0, m) cout &lt;&lt; b&#x5B;i] &lt;&lt; &quot; &quot;; cout &lt;&lt; endl;\r\n    REP(i, m) if (b&#x5B;i]) {\r\n        REP(j, i) if (b&#x5B;j]) {\r\n            okk&#x5B;bi]&#x5B;bj] = okk&#x5B;bj]&#x5B;bi] = ok&#x5B;b&#x5B;i]]&#x5B;b&#x5B;j]];\r\n        }\r\n    }\r\n    DWN(i, m+1, 2) DWN(j, i, 1) {\r\n        s &lt;&lt;= 1; s |= !okk&#x5B;i]&#x5B;j];\r\n    }\r\n    DWN(i, m, 0) {\r\n        s &lt;&lt;= Offset; s |= bi;\r\n    }\r\n    return s;\r\n}\r\nvoid decode(T_state s){\r\n    REP(i, m) {\r\n        b&#x5B;i] = s &amp; Mask;\r\n        s &gt;&gt;= Offset;\r\n    }\r\n    memset(ok, 1, sizeof(ok));\r\n    FOR(i, 2, m+1) FOR(j, 1, i) {\r\n        ok&#x5B;i]&#x5B;j] = ok&#x5B;j]&#x5B;i] = !(s&amp;1);\r\n        s &gt;&gt;= 1;\r\n    }\r\n}\r\nconst int Prime = 9979, MaxSZ = 1 &lt;&lt; 20;\r\nT_key d;\r\ntemplate&lt;class T_state, class T_key&gt; struct hashTable {\r\n    int head&#x5B;Prime]; int next&#x5B;MaxSZ], sz;\r\n    T_state state&#x5B;MaxSZ]; T_key key&#x5B;MaxSZ];\r\n    void clear() {\r\n        sz = 0;\r\n        memset(head, -1, sizeof(head));\r\n    }\r\n    void push(T_state s) {\r\n        int x = s % Prime;\r\n        for (int i = head&#x5B;x]; ~i; i = next&#x5B;i]) {\r\n            if (state&#x5B;i] == s) {\r\n                key&#x5B;i] += d;\r\n                return;\r\n            }\r\n        }\r\n        state&#x5B;sz] = s, key&#x5B;sz] = d;\r\n        next&#x5B;sz] = head&#x5B;x], head&#x5B;x] = sz++;\r\n    }\r\n};\r\nhashTable&lt;T_state, T_key&gt; H&#x5B;2], *H0, *H1;\r\n\r\n\/\/\r\n\/\/ 64 \/\/ 32 16 8 \/\/ . . .\r\n\r\nint j, lf, up;\r\nvoid push(int t) {\r\n    b&#x5B;j] = t;\r\n    \/\/assert(ok&#x5B;t]&#x5B;lf] == ok&#x5B;lf]&#x5B;t]);\r\n    \/\/assert(ok&#x5B;t]&#x5B;up] == ok&#x5B;up]&#x5B;t]);\r\n    bool ok_lf = ok&#x5B;t]&#x5B;lf], ok_up = ok&#x5B;t]&#x5B;up];\r\n    ok&#x5B;t]&#x5B;lf] = ok&#x5B;lf]&#x5B;t] = ok&#x5B;t]&#x5B;up] = ok&#x5B;up]&#x5B;t] = 0;\r\n    H1-&gt;push(encode());\r\n    ok&#x5B;t]&#x5B;lf] = ok&#x5B;lf]&#x5B;t] = ok_lf;\r\n    ok&#x5B;t]&#x5B;up] = ok&#x5B;up]&#x5B;t] = ok_up;\r\n}\r\nvoid push() {\r\n    H1-&gt;push(encode());\r\n}\r\n\r\nvoid init() {\r\n    cin &gt;&gt; n &gt;&gt; m; if (m &gt; n) swap(n, m);\r\n}\r\nvoid solve() {\r\n    H0 = H, H1 = H+1, H1-&gt;clear(), d = 1, H1-&gt;push(0);\r\n\r\n    REP(i, n) {\r\n        REP(j, m) {\r\n            ::j = j; swap(H0, H1), H1-&gt;clear();\r\n            REP(u, H0-&gt;sz) {\r\n                decode(H0-&gt;state&#x5B;u]); d = H0-&gt;key&#x5B;u];\r\n                lf = j ? b&#x5B;j-1] : 0, up = b&#x5B;j];\r\n\r\n                \/*cout &lt;&lt; H0-&gt;state&#x5B;u] &lt;&lt; &quot;: &quot; &lt;&lt; d &lt;&lt; endl;\r\n                REP(i, m) cout &lt;&lt; b&#x5B;i] &lt;&lt; &quot; &quot;; cout &lt;&lt; endl;\r\n                cout &lt;&lt; &quot;c: &quot;; FOR(i, 1, m+1) FOR(j, 1, i) cout &lt;&lt; ok&#x5B;i]&#x5B;j] &lt;&lt; &quot; &quot;; cout &lt;&lt; endl;\r\n*\/\r\n\r\n                push(m+1);\r\n                if (lf &amp;&amp; up) {\r\n                    push(lf);\r\n                    if (lf!=up) {\r\n                        push(up);\r\n                        if (ok&#x5B;lf]&#x5B;up]) {\r\n                            \/\/if (lf &gt; up) swap(lf, up);\r\n                            REP(i, m) if (b&#x5B;i] == up) b&#x5B;i] = lf;\r\n                            FOR(t, 1, m+1) {\r\n                                _ok&#x5B;t] = ok&#x5B;lf]&#x5B;t];\r\n                                if (ok&#x5B;lf]&#x5B;t] &amp;&amp; !ok&#x5B;up]&#x5B;t]) {\r\n                                    ok&#x5B;lf]&#x5B;t] = ok&#x5B;t]&#x5B;lf] = 0;\r\n                                }\r\n                            }\r\n                            push();\r\n                            FOR(t, 1, m+1) ok&#x5B;lf]&#x5B;t] = ok&#x5B;t]&#x5B;lf] = _ok&#x5B;t];\r\n                        }\r\n                    }\r\n                } else if (lf | up) {\r\n                    push(lf|up);\r\n                }\r\n            }\r\n        }\r\n        \/\/H1-&gt;roll();\r\n    }\r\n\r\n    \/*H0 = H1;\r\n    REP(u, H0-&gt;sz) {\r\n        decode(H0-&gt;state&#x5B;u]); d = H0-&gt;key&#x5B;u];\r\n        cout &lt;&lt; H0-&gt;state&#x5B;u] &lt;&lt; &quot;: &quot; &lt;&lt; d &lt;&lt; endl;\r\n        REP(i, m) cout &lt;&lt; b&#x5B;i] &lt;&lt; &quot; &quot;; cout &lt;&lt; endl;\r\n    }*\/\r\n}\r\nvoid print() {\r\n    T_key z = 0; REP(i, H1-&gt;sz) z += H1-&gt;key&#x5B;i];\r\n    cout &lt;&lt; z &lt;&lt; endl;\r\n}\r\nint main(){\r\n#ifndef ONLINE_JUDGE\r\n    \/\/\/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n#endif\r\n    int T; cin &gt;&gt; T; while (T--) {\r\n        init(); solve();\r\n        print();\r\n    }\r\n}\r\n\r\n\/\/ \u4e0d\u8003\u8651\u5207\u75d5\u8ff9\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Vjudge | SPOJ | TG \u5148\u590d\u4e60\u4e00\u4e0b \u63d2\u5934 dp \u5927\u5b57\u5178\u3002\u672c\u9898\u663e\u7136\u4f7f\u7528\u67d3\u8272\u6a21\u578b\u3002\u8f6e\u5ed3\u7ebf\u4e0a\u53ea\u6709 m \u4e2a\u4f4d\u7f6e\uff0c\u800c\u4e14\u4e0d\u9700\u8981 roll() \u64cd\u4f5c\uff08\u4e0d\u8fc7\u751f\u6210\u7684\u65b0\u63d2\u5934\uff0c\u8981\u6c42\u524d\u9762\u7684\u533a\u5206\u5f00\u6765\uff0c\u6240\u4ee5\u6211\u4eec\u8fd8\u662f\u7528 m+1 \u6765\u6807\u8bb0\uff09\u3002\u8fd9\u6837\u641e\u5f97\u8bdd\uff0c2\u00d72 \u6211\u4eec\u4f1a\u7b97\u51fa\u7b54\u6848\u7b49\u4e8e 14\uff0c\u4ed4\u7ec6\u89c2\u5bdf\uff0c\u662f\u56e0\u4e3a\u6211\u4eec\u591a\u7b97\u4e86\u4e0b\u9762\u4e24\u79cd\u4e0d\u5408\u6cd5\u7684\u60c5\u51b5\u3002 \u6211\u4eec\u53d1\u73b0\u8fd9\u7c7b\u60c5\u51b5\u90fd\u662f\u56e0\u4e3a\u5728\u524d\u9762\u7684\u8f6c\u79fb\u4e2d\u4ea7\u751f\u4e86\u523b\u75d5\uff0c\u800c\u4f7f\u5f97\u540e\u7eed\u7684\u63d2\u5934\u5408\u5e76\u4e0d\u5408\u6cd5\uff0c\u9700\u8981\u8fdb\u4e00\u6b65\u7f16\u7801\u3002 \u6211\u4eec\u53ef\u4ee5\u7ed9\u6bcf\u4e2a\u8fde\u901a\u5757\u5f3a\u884c\u4e0a\u4e00\u4e2a\u989c\u8272\uff0c\u4e0d\u76f8\u540c\u7684\u989c\u8272\u4e0d\u53ef\u5408\u5e76\u3002\u4f46\u662f\u56e0\u4e3a\u539f\u95ee\u9898\u6ca1\u6709\u989c\u8272\uff0c\u8fd9\u6837\u8ba1\u6570\u8d77\u6765\u4f1a\u6bd4\u8f83\u9ebb\u70e6\uff08\u9700\u8981\u6700\u5c0f\u67d3\u8272\uff0c\u5e76\u4e14\u5fc5\u987b\u4fdd\u8bc1\u76f8\u540c\u989c\u8272\u7684\u8fde\u901a\u5757\u5fc5\u88ab\u5408\u5e76\uff09\u3002 \u6211\u4eec\u4e5f\u53ef\u4ee5\u66b4\u529b\u8bb0\u5f55 C(5,2) \u4e2a\u8fde\u901a\u5757\u4e4b\u95f4\u662f\u5426\u4ea7\u751f\u8fc7\u523b\u75d5\uff0c\u8fd9\u6837\u5408\u5e76\u7684\u65f6\u5019\u76f4\u63a5\u5224\u65ad\u5373\u53ef\u3002 \u523b\u75d5\u7684\u7ef4\u62a4\uff0c\u9996\u5148\u6bcf\u6b21\u72b6\u6001\u8f6c\u79fb\uff0c\u5f53\u524d\u63d2\u5934\u90fd\u9700\u8981\u548c\u76f8\u90bb\u4e24\u4e2a\u63d2\u5934\uff08\u5de6\u63d2\u5934\u548c\u4e0a\u63d2\u5934\uff09\u90fd\u6807\u8bb0\u523b\u75d5\u3002 \u5176\u6b21\uff0c\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u6700\u5c0f\u8868\u793a\uff0c\u8fde\u901a\u5757\u7684\u8bb0\u53f7\u4f1a\u88ab relable\uff0c\u56e0\u800c\u7f16\u7801\u7684\u65f6\u5019\u4e5f\u9700\u8981\u91cd\u65b0\u6620\u5c04\u3002 \u6700\u540e\u63d2\u5934\u5408\u5e76\u7684\u65f6\u5019\uff0c\u523b\u75d5\u4fe1\u606f\u4e5f\u9700\u8981\u5408\u5e76\u3002 \u6700\u540e\u4e0a\u4e00\u4e0b\u9ad8\u7cbe\u5ea6\u6a21\u677f\u5c31\u597d\u4e86\u3002\u7559\u4e2a OEIS \u5730\u5740\u65b9\u4fbf\u5927\u5bb6 debug\u3002 \/* This code has been written by MinakoKojima, feel free to ask me question. Blog: https:\/\/www.shuizilong.com\/house Template Date: 2015.10.12 Note: &#8230; *\/ #pragma comment(linker, &quot;\/STACK:36777216&quot;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-1660","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-qM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=1660"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1660\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=1660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=1660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=1660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}