#include <iostream>
using namespace std;
int z,l,i,m;
int f(int x){
return (z*x+i)%m;
}
int brent(int x0){
int lam=1,tor=x0,hare=f(x0),power=1;
while(hare!=tor){
if(power==lam){
power*=2;
lam=0;
tor=hare;
}
hare=f(hare);
lam++;
}
return lam;
}
int main(){
int cs=1;
while(1){
cin>>z>>i>>m>>l;
if(!z&&!i&&!m&&!l)
return 0;
cout<<"Case "<<cs++<<": "<<brent(l)<<endl;
}
}
using namespace std;
int z,l,i,m;
int f(int x){
return (z*x+i)%m;
}
int brent(int x0){
int lam=1,tor=x0,hare=f(x0),power=1;
while(hare!=tor){
if(power==lam){
power*=2;
lam=0;
tor=hare;
}
hare=f(hare);
lam++;
}
return lam;
}
int main(){
int cs=1;
while(1){
cin>>z>>i>>m>>l;
if(!z&&!i&&!m&&!l)
return 0;
cout<<"Case "<<cs++<<": "<<brent(l)<<endl;
}
}
Comments
Post a Comment