UVA 350
#include<iostream> #include <algorithm> #include<vector> using namespace std; int main() { int Z,I,M,L,flag=1; while(cin>>Z>>I>>M>>L) { if(Z==0&&I==0&&M==0&&L==0)break; int temp,temp2,i=0; temp=Z*L+I; vector<int>v; v.push_back(L); while(true) { temp2=temp%M; i++; vector<int>::iterator it; it=find(v.begin(),v.end(),temp2); if(it!=v.end()) { cout<<"Case "<<flag<<": "<<i-(it-v.begin())<<endl; break; } else { v.push_back(temp2); temp=Z*temp2+I; } } flag++; } return 0; }