Test instructions
For C's for (i=a; i!=b; I +=c) loop, ask how many loops in the K-bit storage system will end.
If the end is within a finite number of times, the output cycle times.
Otherwise output forever
Ideas:
Easy to list the congruence equation:
X*c+y*2^k = B-a can be solved by expanding GCD
148k0msc++808b#include<cstdio> #include <iostream> #include <algorithm> #include <cstring> Using namespace Std;typedef long long ll;ll a,b,c;ll x,y;int k;ll e_gcd (ll a,ll b,ll &x,ll &y) { ll ans; if (b==0) { ans=a; x=1,y=0; } else { ans=e_gcd (b,a%b,y,x); y-= (A/b) *x; } return ans;} int main () { while (scanf ("%i64d%i64d%i64d%d", &a,&b,&c,&k), (a+b+c+k)) { ll m= (LL) 1 <<k; ll r= (b-a+m)%m; ll D=E_GCD (c,m,x,y); if (r%d) puts ("FOREVER"); else { ll ans=x* (r/d); ll t=m/d; Ans= (ans%t+t)%t; printf ("%i64d\n", ans); } } return 0;}
POJ 2115-c looooops (basic unary linear with congruence)