标签:
ax=b (mod n)
该方程有解的充要条件为 gcd(a,n) | b ,即 b% gcd(a,n)==0
令d=gcd(a,n)
有该方程的 最小整数解为 x = e (mod n/d)
其中e = [x0 mod(n/d) + n/d] mod (n/d) ,x0为方程的最小解
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long LL; void Exgcd(LL a, LL b, LL& d, LL& x, LL& y) { if(b == 0) {d = a, x = 1, y = 0;} else Exgcd(b,a%b,d,y,x),y -= x*(a/b); } int main() { LL A,B,C,K,D; while(cin >> A >> B >> C >> K && (A + B + C + K)) { LL X,Y; LL tmp = B - A; LL bb = 1LL << K; Exgcd(C,bb,D,X,Y); if(tmp % D) cout << "FOREVER\n"; else { X = tmp / D * X; LL tt = bb / D; cout << (X%tt+ tt) % tt << endl; } } }
标签:
原文地址:http://www.cnblogs.com/aoxuets/p/4704056.html