题意:
for (variable = A; variable != B; variable += C)
statement;
给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER".
思路:
需要求解 (A + x * C) % mod = B
变形之后即 C * x + mod * y = B - A = gcd(C , mod) * [ (B - A) / gcd(C , mod) ]
用扩展欧几里德定理 需要求 C * x + mod * y = gcd(C , mod)
a = C, b = mod
即模线性方程
需要注意的是: 把模线性方程求得的特解转化为正数之后,要模 b/gcd(a,b) [而不是b]***,解释如下:
因为求得的解的含义是"步数",所以要模"步数对应的周期".
#include <cstdio> using namespace std; typedef long long ll; ll Extended_Euclid(ll a, ll b, ll &x, ll &y) { if(!b) { x = 1; y = 0;//无关变量取0方便计算,求模之后无影响 return a; } ll r = Extended_Euclid(b, a%b, x, y); ll t = x; x = y; y = t - a/b*y; return r; } ll Modular_Linear_Equation(ll a, ll b, ll n) { ll x, y, e; ll d = Extended_Euclid(a, n, x, y); if(b % d) return -1; e = x*b/d % n + n;//转化为正数,要先取模再加 return e % (n/d);//*** } int main() { ll a,b,c,ans; int k; while(scanf("%lld %lld %lld %d",&a,&b,&c,&k) && (a+b+c+k)) { ans = Modular_Linear_Equation(c, b-a, ((ll)1)<<k); if(ans == -1) puts("FOREVER"); else printf("%lld\n",ans); } }