#include<iostream>
using namespace std;
int main()
{
int Z,I,M,L,i,n,check,point,a[10000];
n=1;
while(cin>>Z>>I>>M>>L)
{
if(Z==0&&I==0&&M==0&&L==0)break;
point=0;check=1;
while(check)
{
a[point++]=L;
L=(Z*L+I)%M;
for(i=0;i<point;i++)
if(a[i]==L)
{
check=0;break;
}
}
cout<<"Case "<<n++<<": "<<point-i<<endl;
}
}
更快的:
#include <bits/stdc++.h>
using namespace std; int main(int argc, char** argv){ //ios_base::sync_with_stdio(false); cin.tie(0); int a[4],b=1,c,d,e[10000],g; while(cin>>a[0]>>a[1]>>a[2]>>a[3]&&a[0]+a[1]+a[2]+a[3]!=0&&b++){ g=1; d=0; e[d]=a[3]; while(g){ e[d]=(a[0]*e[d-1]+a[1])%a[2]; for(int f=0;f<=d-1;f++){ if(e[f]==e[d]){ f=d; g=0; } } d++; } cout<<"Case "<<b-1<<": "<<d-1<<'\n'; } }
AC (14ms, 360KB) |