We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<unordered_set>usingnamespacestd;intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intN,S,P,Q,n,count=1;cin>>N>>S>>P>>Q;n=pow(2,31);// int constant = Q % n;inttortoise=S%n;inthare=(tortoise*P+Q)%n;inthare_1,hare_2;while(hare!=tortoise){if(count==N)break;hare_1=(hare*P+Q)%n;hare_2=(hare_1*P+Q)%n;hare=hare_2;tortoise=(tortoise*P+Q)%n;count+=1;}if(hare_1==hare_2)count-=1;cout<<count<<endl;return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bit Array
You are viewing a single comment's thread. Return to all comments →