Test Instructions:Link
Method:Bsgs
parsing:First he gives you a recursive sequence of numbers in the mod p sense. So we can find out the general term. X N+1 +k=a?( X n +k) So b = ( a 1 span class= "Mo" id= "mathjax-span-1135" style= "Font-family:mathjax_main;" >) k Then we can solve it. K then what is the general term of this series? X n =a n 1 ?( X 1 +k)?k Given in the question X n To find out n is ok. So I just need to move the item. There is a problem here, when our general formula does not contain the first item, so we need to give the first sentence, and also the first item is a constant term.
Code:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MOD 140345Using namespace Std;typedef long long ll;intt,cnt;intHead[mod+Ten];ll ans;struct node{ll From,to,val;int Next;} Edge[mod+Ten];void init () {memset (head,-1, sizeof (head)); Cnt=1;} void Edgeadd (ll from,ll to,ll val) {edge[cnt].from=from,edge[cnt].to=to,edge[cnt].val=val; EDGE[CNT].Next=head[from]; head[from]=cnt++;} void Exgcd (ll a,ll B,ll &x, LL &y, LL &GCD) {if(!B) {x=1,y=0, Gcd=a;return; } EXGCD (B,a%b,y,x, GCD);y=y-a/b*x;} ll Get_inv (LLx, ll MOD) {ll x,y,gcd; EXGCD (x, MOD,X,Y,GCD);return(X%mod+MOD)%mod;} void Bsgs (ll a,ll b,ll C) {init (); llm=(int) Ceil (sqrt(C)); ll k=1; for(intI=0;i<m; i++) {intflag=1; for(intJ=head[k%mod];j!=-1; J=edge[j].Next) {if(edge[j].val==k) {flag=0; Break;} }if(flag) Edgeadd (k%mod, i,k); K=k*a%c; } ll Invk=get_inv (K,C); ll invd=1; for(intI=0; i<=m; i++) {ll tmpb=b*INVD%c; for(intJ=head[tmpb%mod];j!=-1; J=edge[j].Next) {if(EDGE[J].VAL==TMPB) {Ans=edge[j].to+i*m;return;} } INVD=INVD*INVK%c; }}ll p,a,b,x1,t;intMain () {scanf ("%d", &t); while(t--) {scanf ("%lld%lld%lld%lld%lld", &p,&a,&b,&x1,&t);if(x1==t) {Puts ("1");Continue;}if(a==0) {if(x1==t) puts ("1");Else if(b==t) puts ("2");ElsePuts"-1"); }Else if(a==1) {t= (t-x1)%p+P)%p; ll X,Y,GCD; EXGCD (B,P,X,Y,GCD);if(t%GCD!=0) puts ("-1");Else printf("%lld\ n", ((X*t/GCD)%(P/GCD) + (P/GCD))%(P/GCD) +1); }Else{ll k=b*GET_INV(A-1, p)%p; T= (T+k)%p*GET_INV(x1+k,p)%p; ans=-1; BSGS (A,T,P);if(ans!=-1)printf("%lld\ n", ans+1);ElsePuts"-1"); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 3122 [Sdoi2013] random number generator bsgs