Bzoj 3122 Random number generator

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=3122

Test instructions: Give the p,a,b,x1,t

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.

1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6#include <map>7 #definell Long Long8 ll P;9 ll Read () {Ten     CharCh=getchar (); ll t=0, f=1; One      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A      while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} -     returnt*F; - } the ll Pow (ll X,ll y) { -ll res=1; -     if(x<0) x= (x+p)%p; -      while(y) { +         if(y%2) res= (res*x)%p; -x= (x*x)%p; +Y/=2; A     } at     returnRes; - } - ll gcd (ll A,ll b) { -     if(b==0)returnA; -     Else returnGCD (b,a%b); - } in voidEXGCD (ll a,ll b,ll &x,ll &y) { -     if(b==0){ tox=1; +y=0; -         return; the     } *EXGCD (b,a%b,x,y); $ll t=x;Panax Notoginsengx=y; -Y=t-(A/b) *y; the } + ll Reverse (ll X) { All a=x,b=p; the ll x, y; + EXGCD (a,b,x,y); -     return(x%p+p)%p; $ } $ ll work (ll A,ll b) { -a%=p; -     if(a==0){ the         if(b==0)return 1; -         Else return-1;Wuyi     } theStd::map<ll,int>MP; -ll M=sqrt (p) +1, i=1, Im=pow (a,p-1-m), t=1; WuMp.clear (); mp[1]=m+1; -      for(intI=1; i<m;i++){ Aboutt=t*a%p; $         if(!mp[t]) mp[t]=i;  -     } -      for(intk=0; k<m;k++){ -         inti=mp[i*b%p]; A         if(i) { +             if(i==m+1) i=0; the             returni+k*m; -         } $i=i*im%p; the     } the     return-1; the } the ll solve (ll a,ll b,ll x1,ll t) { -     if(t==x1) { in             return 1; the     } the     if(a==0){ About             if(b==t) { the                 return 2; the             } the             return-1; +     } -     if(a==1){ thell a=b,b=p,t= (t-x1+p)%p;Bayill d=gcd (A, b); the             if(t%D) { the                 return-1; -             } -T/=D; the ll x, y; the EXGCD (a,b,x,y); thex= (x*t)%p; the              while(x<0) x+=p; -             returnx+1; the     } thell C=pow (A-1, P-2); thell a= (b*c+x1)%p,b=p,t= (b*c+t)%p;94     if(a<0) a= (a+p)%p; the     if(b<0) b= (b+p)%p; thell d=gcd (A, b); the     if(t%D) {98         return-1; About     } -T/=D;101 ll x, y;102 EXGCD (a,b,x,y);103      while(x<0) x= (x+p)%p;104x= (x*t)%p; thell ans=Work (a,x);106     if(ans!=-1)returnans+1;107     Else returnans;108 }109 intMain () { the     intTcase;111scanf"%d",&Tcase); the      while(tcase--){113 ll A,b,x1,t; theP=read (); A=read (); B=read (); X1=read (); t=read (); theprintf"%lld\n", Solve (a,b,x1,t)); the     }117}

Bzoj 3122 Random number generator

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.