"Bzoj 3122" [Sdoi2013] random number generator (BSGS)

Source: Internet
Author: User

3122: [Sdoi2013] random number generator time limit:10 Sec Memory limit:256 MB
submit:1442 solved:552 Description

Input

The input contains multiple sets of data, and the first line is a positive integer t, indicating the number of data groups within the test point.

Next T line, each line has five integer p,a,b,x1,t, which represents a set of data. Ensure that both X1 and T are legal page numbers.

Note: P must be prime

Output

A total of T-lines, one integer per line, indicating which day he first read page T. If he would never read page T, output-1.

Sample Input 3
7 1 1) 3 3
7 2 2) 2 0
7 2 2) 2 1


Sample Output
1
3
-1
HINT

0<=a<=p-1,0<=b<=p-1,2<=p<=10^9

Analysis

This problem is almost the pit Father home!! P is a prime number does not say, there are all kinds of disgusting special!!!!

A=0,a=1,b=0 are to be condemned.

Although the introduction of the general formula, but where the division can be reversed where can not know, a beginning-pass various wa!!

Look at other people's puzzle:

Known xn=a*xn-1+b%p, to find the smallest n order xn=t

First, if x1=t, returns 1

If a=0, then if B=t returns 2, no solution

If a=1, then t=t-x1+p%p, you can list the equation

B*x+p*y==t% P

If a>=2, then according to geometric series and available

xn=t=x1*a^ (n-1) +b* (a^ (n-1)-1)/(A-1)%p

Because P is prime, so c=inv[a-1]= (A-1) ^ (p-2)

x1*a^ (n-1) +b*c* (a^ (n-1)) ==b*c+t%p

(x1+b*c) * (a^ (n-1)) ==b*c+t% p

Make A=x1+b*c,b=p,c=b*c+t

Is the solution A*x+b*y==c%p

Solution out x=a^ (n-1), then this with bsgs to ask for it.

Http://www.cnblogs.com/qzqzgfy/p/5581955.html

My Code:

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7#include <cmath>8 using namespacestd;9 #defineLL Long LongTen #defineMAXN 35000 One  A structnode - { - LL Id,val; the }T[MAXN]; -  - LL Ax,ay; - ll EXGCD (ll A,ll b) + { -     if(b==0) {ax=1; ay=0;returnA;} +LL G=EXGCD (b,a%b); ALL xx=Ax; atAx=ay;ay=xx-(A/b) *ay; -     returnG; - } -  - BOOLCMP (node X,node y) {return(X.val==y.val)? (x.id<y.id):(x.val<y.val);} -  in LL CNT; -  to ll T_div (ll x) + { -LL l=0, r=CNT; the      while(l<R) *     { $LL mid= (l+r) >>1;Panax Notoginseng         if(t[mid].val==x)returnt[mid].id; -         if(t[mid].val>x) r=mid-1; the         ElseL=mid+1; +     } A     if(t[l].val==x)returnt[l].id; the     return-1; + } -  $ ll Get_ans (ll k,ll a,ll c,ll p) $ { -ll sq= (LL) ceil (sqrt (Double) (p)); -t[0].id=0; t[0].val=k%p; the      for(LL i=1; i<=sq;i++) t[i].val= (t[i-1].val*a)%p,t[i].id=i; -     WuyiSort (t+1, t+1+sq,cmp); theCnt=0; -      for(LL i=1; i<=sq;i++)if(t[i].val!=t[i-1].val) t[++cnt]=T[i]; Wu      -LL bm=1; About      for(LL i=1; i<=sq;i++) bm= (bm*a)%p; $LL g=EXGCD (bm,p); -Ax= (ax% (p/g) + (p/g))% (p/g); -bm=Ax; -      ALL tmp=c%p; +      for(LL i=0; i<=sq;i++) the     { -LL now=T_div (TMP); $         if(now!=-1)returnnow+i*sq; thetmp= (TMP*BM)%p; the     } the     return-1; the } -  in ll Ffind (ll k,ll a,ll c,ll p) the { theLL x=K; About      for(LL i=0; i<= -; i++) the     { the         if(X==C)returni; thex= (x*a)%p; +     } - LL G; thex=0;Bayi      while((G=EXGCD (a,p))! =1) the     { theP/=G; -K= (k* (a/g))%p; -         if(c%g!=0)return-1; theC/=G; theX + +; the     } theLL ans=Get_ans (k,a,c,p); -     if(ans==-1)return-1; the     returnans+x; the } the 94 intMain () the { the     intT,kase=0; thescanf"%d",&T);98      while(t--) About     { -         //printf ("%d:", ++kase);101 LL p,a,b,x,t,c;102scanf"%lld%lld%lld%lld%lld",&p,&a,&b,&x,&t);103         //if (b==0) {printf ("1\n"); continue;}104         if(t==x) {printf ("1\n");Continue;} the         if(a==0&&b==t) {printf ("2\n");Continue;}106         Else if(a==0) {printf ("-1\n");Continue;}107         if(a==1)108         {109LL g=EXGCD (b,p); thec=t-x;c= (c%p+p)%p;111             if(c%g!=0) {printf ("-1\n");Continue;} theax*=c/G;113Ax= (ax% (p/g) + (p/g))% (p/g); theprintf"%d\n", ax+1); the             Continue; the         }117         118 LL Phi;119         if(B% (A-1)==0) phi=b/(A-1); -         Else121         {122             if(EXGCD (A-1, p)! =1) {printf ("-1\n");Continue;}123ax= (ax%p+p)%p;124phi= (b*ax)%p; the         }126LL a=x+phi,b=phi+T;127          -         if(b==0) a=x,b=T;129          theA= (a%p+p)%p; b= (b%p+p)%p;131         //if (a==0) {printf (" -1\n"); continue;} the         133LL ans=Ffind (a,a,b,p);134         135         if(ans==-1) printf ("-1\n");136         Elseprintf"%lld\n", ans+1);137     }138     return 0;139}
Bzoj 3211

2016-09-05 18:24:44

"Bzoj 3122" [Sdoi2013] random number generator (BSGS)

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.