Test instructions: Given the initial value of the For loop, the end value and increment, and a modulo, the minimum number of cycles.
Analysis:
After reading the question should know is a concept of congruence, so it is to solve a one-time congruence equation, like the above problem with the expansion of Euclidean algorithm. The trick point of the problem is K Max is 32, then 2^32 out of int, to use a long long, so in 1<<k to do this: 1ll<<k, or WA.
Code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < string> #include <queue> #define INF 1000000007using namespace Std;long long X,y,m,n,l;long long A,b,c,d;long Long P,q,ans;long long gcd (long long A,long long B) {if (b==0) return a;else return gcd (b,a%b);} void Exgcd (Long long A,long long B,long long &p,long long &q) {if (b==0) {P=1;q=0;return;} ELSE{EXGCD (b,a%b,q,p); q-=a/b*p;}} int main () {while (cin>>x>>y>>m>>n) {if (!x&&!y&&!m&&!n) break;a=m;b =1LL<<N;C=Y-X;D=GCD (A, b); if (c%d) {cout<< "FOREVER" <<ENDL;} Else{long long TMP=B;A/=D;B/=D;C/=D;EXGCD (a,b,p,q);//cout<<p<< "" <<q<<endl;p*=c;ans=p%b; if (ans<0) Ans+=b;cout<<ans<<endl;}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2115 for loop count-number theory-(congruence equation + extended Euclidean algorithm)