3122: [Sdoi2013] random number generator time limit: ten Sec Memory Limit:
Submit: 1204 Solved: 471
[Submit] [Status] [Discuss] 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 Input3
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
expand Euclid to find the inverse of the Yuan +bsgs
The sticking point here: http://hzwer.com/6963.html
(Formula phobia drift over, do not take a piece of cloud)
Attention to a variety of special cases of judgment, WA many times ...
#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring > #include <algorithm> #include <map> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i= j;i>=n;i--) #define LL long longusing namespace std;ll t,p,a,b,x1,t;map<ll,int> mp;inline int read () {int x=0,f=1 ; Char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline ll Getpow (ll x,ll y,ll p) {ll ret=1;for (; y;y>>=1,x=x*x%p) if (y&1) ret=ret*x%p;return ret;} inline ll EXGCD (ll a,ll b,ll &x,ll &y) {if (!b) {X=1;y=0;return A;} ll RET=EXGCD (b,a%b,x,y); ll t=x;x=y;y=t-a/b*y;return ret;} inline ll Bsgs (ll a,ll b,ll p) {if (b==1) return p-1;ll M=ceil (sqrt (p)); Mp.clear (); ll Tmp=1,inv=getpow (a,p-2,p); F (i,0,m) {ll h=b*tmp%p;if (!mp[h]) mp[h]=i;tmp=tmp*inv%p;} Tmp=1;ll Base=getpow (a,m,p); F (i,0,m) {if (mp[tmp]) {ll ret=i*m+mp[tmp];return ret;} Tmp=tmp*base%p;} return-1;} inline ll Calc1 () {ll c= ((t-x1)%p+p)%p,x,y;ll tmp=exgcd (b,p,x,y); if (c%tmp) Return-1;return (x*c/tmp%p+p)%p+1;} inline ll Calc2 () {ll c=getpow (a-1,p-2,p); ll t1= (b*c%p+x1)%p,t2= (b*c%p+t)%p,x,y;if (t1==0) {if (t2==0) return 1;else return-1;} if (t2==0) return-1;ll tmp=exgcd (t1,p,x,y), if (t2%tmp) return 0;t2/=tmp;x= (x%p+p)%p;ll t=bsgs (a,x*t2%p,p); if (t==-1) Return-1;else return t+1;} inline ll Getans () {if (x1==t) return 1;if (a==0) return b==t?2:-1;if (a==1) return Calc1 (); return calc2 ();} int main () {int t=read (), while (t--) {p=read (); A=read (); B=read (); X1=read (); T=read ();p rintf ("%lld\n", Getans ());}}
bzoj3122 "SDOI2013" random number generator