P2162--Zrn god Ben's Origami game
Time limit: 2000MS
Memory Limit: 524288KB
Description
Zrn God Ben recently liked an origami game, so he folded it almost every day with a long, long, lonely piece of paper tape. The specific rules are this:
This is a note with a length of N and a width of 1, starting from 1 to write a continuous n natural number.
If its length is even, the Zrn god Ben will be happy to fold it directly from left to right or from right to left. For example, a note with a length of 6 will be folded from left to right:
If its length is prime, zrn God Ben will feel not very good, he can only the left end of the right fold or the right side of the left fold. For example, a note with a length of 5 is folded from right to left, which turns out to be:
If its length is composite, Zrn God Ben will find its smallest quality factor k, divide it into the same length of the K segment, according to the length of the K-note method processing.
Zrn God Ben in this way over and over, and finally folded the note into a stacked small square.
The following shows the folding of a strip of paper with a length of 15, folded in half and a prime number, from right to left: (the description may not be clear, please forgive me)
(length is 15)
(length is 10)
(length is 5)
(Length is 4)
(Length is 2)
(length 1, target State)
Input Format
Three number n,p1,p2,n for the length of the note, if p1=1, fold from left to right, if p1=2, fold from right to left, if p2=1, length is prime when the left end of the fold, if p2=2, the length of prime number when the right end of the fold.
Output Format
n rows, one integer per line, is an arrangement of 1~n, indicating the number of the target State from top to bottom.
Sample Input
15 2 2
Sample Output
212981334147615510111
Hint
For 20% of data, n≤40;
For 50% of data, n≤1000;
For 100% of data, n≤5000000.
The "Puzzle" was a simulation, the data range scared silly
And then through the WZT, Cyx, the great gathering of the teachings
Finally understood-this is a double-linked list.
Then just follow the questions.
Suppressed for more than half an hour, found that the element factor was I sieve wrong ...
1#include <bits/stdc++.h>2 using namespacestd;3 Const intmax=5000010;4 intn,p1,p2,pr[max],head[max],next[max][2];5 intBgn,end,len;6 intMain () {7scanf"%d%d%d",&n,&p1,&p2);8 //P[i]: maximum prime factor of I9 //If I is a prime number, then p[i]=i;Ten for(intI=2; i<=n;++i) One if(pr[i]==0) { Apr[i]=i; - for(intj=i*2; j<=n;j+=i) - if(pr[j]==0) pr[j]=i; the } -bgn=1; end=len=N; - for(intI=1; i<=n;++i) -Head[i]=i, next[i][0]=next[i][1]=0; + while(len!=1) { - intsxbk=len/Pr[len]; + if(len&1? p2==1:p 1==1) { A for(intj=bgn+sxbk-1, k=bgn+sxbk;j>=bgn;--j,++k) { atnext[head[k]][k&1]=Head[j]; -next[head[j]][j&1]=Head[k]; -head[k]=J; - } -bgn+=SXBK; - } in Else { - for(intj=end-sxbk+1, K=end-sxbk;j<=end;++j,--k) { tonext[head[k]][k&1]=Head[j]; +next[head[j]][j&1]=Head[k]; -head[k]=J; the } *end-=SXBK; $ }Panax Notoginsenglen-=SXBK; - } the for(inti=head[bgn];i;i=next[i][head[bgn]&1]) +printf"%d\n", i); A return 0; the}
View Code
[Fzyzoj 2162] Zrn God Ben's Origami game