http://www.elijahqi.win/archives/2890
题目描述
Polycarpus has recently got interested in sequences of pseudorandom numbers. He learned that many programming languages generate such sequences in a similar way: (for
i>=1
i>=1 ). Here
a
a ,
b
b ,
m
m are constants, fixed for the given realization of the pseudorandom numbers generator,
r_{0}
r0 is the so-called
randseed
randseed (this value can be set from the program using functions like RandSeed(r) or srand(n)), and denotes the operation of taking the remainder of division.
For example, if
a=2,b=6,m=12,r_{0}=11
a=2,b=6,m=12,r0=11 , the generated sequence will be:
4,2,10,2,10,2,10,2,10,2,10,…
4,2,10,2,10,2,10,2,10,2,10,… .
Polycarpus realized that any such sequence will sooner or later form a cycle, but the cycle may occur not in the beginning, so there exist a preperiod and a period. The example above shows a preperiod equal to 1 and a period equal to 2.
Your task is to find the period of a sequence defined by the given values of
a,b,m
a,b,m and
r_{0}
r0 . Formally, you have to find such minimum positive integer
t
t , for which exists such positive integer
k
k , that for any
i>=k
i>=k :
r_{i}=r_{i+t}
ri=ri+t .
输入输出格式
输入格式:
The single line of the input contains four integers
a
a ,
b
b ,
m
m and
r_{0}
r0 ( 1<=m<=10^{5},0<=a,b<=1000,0<=r_{0}<m
1<=m<=10^{5},0<=a,b<=1000,0<=r_{0}<m
输出格式:
Print a single integer — the period of the sequence.
输入输出样例
输入样例#1: 复制
2 6 12 11
输出样例#1: 复制
2
输入样例#2: 复制
2 3 5 1
输出样例#2: 复制
4
输入样例#3: 复制
3 6 81 9
输出样例#3: 复制
1
说明
The first sample is described above.
In the second sample the sequence is (starting from the first element):
0,3,4,1,0,3,4,1,0,…
0,3,4,1,0,3,4,1,0,…
In the third sample the sequence is (starting from the first element):
33,24,78,78,78,78,…
33,24,78,78,78,78,…
单纯形学不会..
实在困了就只能写写这样的水题..
求伪随机数循环节的长度 注意可能不是从1开始
#include<cstdio>
#include<cctype>
#define ll long long
#include<algorithm>
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
int flag[100010];int a,b,m,r,i;
int main(){
// freopen("cf172b.in","r",stdin);
a=read();b=read();m=read();r=read();
for (i=1;i<=1e8;++i){
r=(a*r+b)%m;if (flag[r]) break;
else flag[r]=i; //printf("%d,r);
}//printf("%d %d\n",r,flag[r]);
printf("%d\n",i-flag[r]);
return 0;
}