Description
After NOI2014, small h has discovered a new method of generating random numbers.
First, given three random seed p,c1,c2 (C1≤C2), a sequence of {Xi},{xi} is generated to satisfy
For any i≥0, the following recursive xi+2= (XI+1+XI) mod P is satisfied
Among them x0=c1, X1=C2.
Then, using the sequence {XI}, you can generate a sequence {AI}. The sequence {AI} is generated in the following way
For any i≥1,ai=∑xj^2 (0≤j≤i) mod P
Then, given a positive integer q, the small H will perform the following operations: Select two positive integers x, y each time to swap ax and ay.
Small h wants to check if the sequence {AI} is random. He still wants to use the method of NOI2014 to generate a nxm matrix where n is the row and M represents the column. We remember that the matrix is D, the 1th line of D is placed in A1~am, the 2nd line is placed in am+1~a2m, and so on, and so on, and so on, the number of the first row J of the Matrix D di,j should be a (i-1) *m+j. Then, like a square to take the number, from the top left corner of the matrix (n,m) to the lower right (), the way can only go to the right or down, so that he passed the number of squares should be (n+m-1). The numbers of each square are then extracted in the order in which they are made, and a sequence is called a sequence of paths. He wants to know what the smallest sequence of paths he can get from the dictionary sequence. Note: For convenience, if the current grid is the same as the number of squares down and to the right, then we think the number of squares down is less than the number of squares to the right.
Input
The first behavior of the input is six positive integer n,m,q,p,c1,c2. meaning See topic Description.
The next Q line, one action per line: contains two positive integers x, Y.
Output
Outputs a row, a total of (n+m-1) positive integers, representing the path sequence with the smallest dictionary order.
Order F[-1]=1,f[0]=0,f[i]=f[i-1]+f[i-2] (i>0)
Yi A[i] (initial) = (c1^2* (f[n]*f[n-1]+1) +c2^2*f[n]*f[n+1]+2*c1*c2* (n is an even number?) F[N]^2:F[N-1]*F[N+1]) mod p
The Fibonacci sequence can be calculated by multiplying O (LOGN)
Minimum dictionary order can be greedy (data is weak, can not consider the same value)
#include <cstdio>typedefLong Longi64;intN,m,q,p,c1,c2;inlineintRead () {intx=0, c=GetChar (); while(c> $|| c< -) c=GetChar (); while(c> -&&c< -) x=x*Ten+c- -, c=GetChar (); returnx;} Inline i64 read64 () {i64 x=0; intC=GetChar (); while(c> $|| c< -) c=GetChar (); while(c> -&&c< -) x=x*Ten+c- -, c=GetChar (); returnx;}intstk[ +],stp=0;intPrintintx) { if(!x) stk[stp++]=0; while(x) stk[stp++]=x%Ten, x/=Ten; for(inti=stp-1; i>=0; i--) Putchar (stk[i]+ -); STP=0;} InlinevoidCalintAintBintCintDint&e,int&f) {e= (i64 (b-a) *c+i64 (a) *d)%Q; F= (i64 (a) *c+i64 (b) *d)%Q; if(e<0) e+=Q; if(f<0) f+=P;}structseq_a{int as[ -],bs[ -]; I64 c12,c22,c1c2; voidinit () { as[0]=1, bs[0]=1; for(intI=1;i< -; i++) Cal ( as[I1],bs[i-1], as[I1],bs[i-1], as[i],bs[i]); C12=I64 (C1) *c1%P; C22=i64 (C2) *c2%P; C1C2=I64 (C1) *c2*2%P; } int operator[] (i64 x) {i64 v1,v2,v3; BOOLd=x&1; --x; intA=0, b=1; for(intI=0; x;i++,x>>=1)if(x&1) Cal (A, B, as[I],bs[i],a,b]; V1=a;v2=b;v3=a+b; if(d)return((v1*v2+1)%p*c12%p+v2*v3%p*c22%p+v1*v3%p*c1c2%p)%P; return((v1*v2+1)%p*c12%p+v2*v3%p*c22%p+v2*v2%p*c1c2%p)%Q; }}a0;namespacemap{Const intp=1844677; I64 Xs[p]; intYs[p]; BOOLEd[p]; int&Get(i64 x) {unsignedintw=x%p; while(Ed[w]) {if(xs[w]==x)returnYs[w]; W+=1237; if(w>=p) w-=Q; } Ed[w]=1; XS[W]=x; YS[W]=A0[x]; if(ys[w]<0) ys[w]+=Q; returnYs[w]; }}intMain () {n=read (); M=read (); Q=read (); P=read (); C1=read (); c2=read (); A0.init (); for(intI=0; i<q;i++) {i64 a=read64 (), b=read64 (); int&a1=map::Get(a), &a2=map::Get(b), A3; A3=a1;a1=a2;a2=A3; } intxp=1, yp=1, ans; Print (Map::Get(1)); while(xp<n| | yp<m) { if(xp==n) + +YP; Else if(yp==m) + +XP; Else{ intA1=map::Get(I64 (XP) *M+YP), A2=map::Get(I64 (xp-1) *m+yp+1); if(A1<A2) + +XP; Else++YP; } Putchar ( +); Print (Map::Get(I64 (xp-1) *m+yp) +p)%q); } return 0;}
bzoj4471 Random number generator Ⅱ