给定r0不断计算出随机数,产生新的r0,当产生的r0与之前的r0出现重复时停止
应当注意:
1.a、c、m、r这些都用long表示 单独强制转换有点不靠谱
2.当只产生一个数的时候(即后面的数都等于r0) 此时应该输出0....
数据 1 0 2 1 应该输出0
#include<iostream>
#include<stdio.h>
using namespace std;
#define MAX(a,b) ((a)>(b)?(a):(b))
typedef unsigned int i64;
bool iscre[16000002];
int main()
{
i64 a,c,m,r0,max=0,last;
scanf("%u%u%u%u",&a,&c,&m,&r0);
while(1)
{
iscre[r0%m] = true;
r0=(a*r0 + c)%m;
if(iscre[r0]) break;
}
last = -1;
for(int i=0;i<m;++i)
if(last != -1 && iscre[i])
{
max = MAX(max,i-last);
last = i;
}
else if(iscre[i])
last = i;
printf("%u\n",max);
return 0;
}