HDU 5535 Cake structure + memory Search

Source: Internet
Author: User

Links:http://acm.hdu.edu.cn/showproblem.php?pid=5355

Test instructions: Given N and M, wherein 1<= n <= 1e5,2 <= m <= 10; Ask if there is a number of 1~n divided into M-group, so that each group is equal; if the output M line is present, each row represents a set of values, otherwise output no;

PS: A total of t<=1000 group data, or quite large;

Idea: After the pre-judgment, if possible there is a direct 2m for the period, from large to small structure and the same m-group, so that the value of n can be reduced to 2m~4m-2; because when n = 4m-1, subtract one period again, the right boundary of the next discussion is 2m-1, and it is easy to know (2m-1) *2m/2/ M is able to construct a conforming m-formula; and the pit father is, the question of the constant coefficient is not too large, the AC code runs 514MS, when the N-cycle is changed to n > 4*m-1, the direct tle; In search of the coefficient is more than one times;

DFS is also more ingenious, need to add a start to monotone find each group of data, is the final all M-group all finished and then return true, not each group completed directly return, so you can also modify the direct selection of errors ;

Determine that a group has finished simply restoring the value of the parameter to its original value, and the need to use DP optimization to set the define to shorthand;

1#include <bits/stdc++.h>2 using namespacestd;3 #defineRep0 (I,L,R) for (int i = (l); i < (R); i++)4 #defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)5 #defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)6 #defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)7 #defineMS0 (a) memset (A,0,sizeof (a))8 #defineMS1 (a) memset (A,-1,sizeof (a))9 #defineMSi (a) memset (A,0x3f,sizeof (a))Ten #defineINF 0x3f3f3f3f One #defineLson L, M, RT << 1 A #defineRson m+1, R, RT << 1|1 - #defineLowbit (x) (x& (×)) -typedef pair<int,int>PII; the #defineA First - #defineB Second - #defineMK Make_pair -typedefLong Longll; +typedef unsignedint UINT; -Template<typename t> + voidRead1 (T &m) A { atT x=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -m = x*F; - } -Template<typename t> in voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} -Template<typename t> to voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} +Template<typename t> - void  out(T a) the { *     if(a>9) out(ATen); $Putchar (a%Ten+'0');Panax Notoginseng } - intI,j,k,n,m,l,r; the Const intN = 1e5+7; + intans[ One][n],aux[ One][n],vs[ -],ave,board; A intdp[ -][ One],rec[ -][ One][ One][ -]; the #definedef Rec[board][m] + BOOLDfsintTotintIdintstart) - { $     if(Tot = Ave) {Id++;tot =0; start =1;}//This is the end of an item, the beginning of another; of course. Returns true only when all is finished; $     if(id = =m) { -REP1 (I,1, board)if(!Vs[i]) { -aux[id][++aux[id][0]] =i; the         } -         return true;Wuyi     } the Rep1 (i,start,board) { -         if(Tot+i > Ave)return false; Wu         if(!Vs[i]) { -Vs[i] =1; Aboutaux[id][++aux[id][0]] =i; $             if(Dfs (tot+i,id,i+1))return true; -Vs[i] =0, aux[id][0]--; -         } -     } A     return false; + } the BOOLSolveinttop) - { $     if(Top >=4*m-1){ the          for(inti =1, j =0; I <= m;i++,j++){ theans[i][++ans[i][0]] = top-2*m+1+J; theans[i][++ans[i][0]] = top-J; the         } -        returnSolve (top-2*m); in     } the     Else{ theAve = top* (top+1)/m/2; About         //printf ("%d", ave); theboard =top; the         if(Dp[board][m] = =0){ the             if(Dfs (0,1,1)){ +DP[BOARD][M] =1; -REP1 (I,1, M) theRep1 (J,0, aux[i][0])BayiDEF[I][J] = Aux[i][j];//define. the             } the             ElseDP[BOARD][M] =-1; -         } -         if(Dp[board][m] = =1)return true; the         return false; the     } the } the intMain () - { the     //freopen ("Data.txt", "R", stdin); the     //freopen ("OUT.txt", "w", stdout); the     intt; read1 (t);94      for(intKase =1; Kase <= t;kase++){ the MS0 (VS); theREP1 (I,0,Ten) ans[i][0] = aux[i][0] =0; the read2 (n,m);98ll sum = 1ll*n* (n+1)/2; About         if(Sum%m | | sum/m < n | |!solve (n)) { -Puts"NO");101             Continue;102         }103Puts"YES");104REP1 (I,1, M) { theprintf"%d", ans[i][0]+def[i][0]);106Rep1 (J,1, ans[i][0])107printf"%d", Ans[i][j]);108Rep1 (J,1, def[i][0])109printf"%d", Def[i][j]); thePuts"");111         } the     }113     return 0; the}

HDU 5535 Cake structure + memory Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.