POJ 2115

来源:互联网 发布:萧然网络问政瓜沥 编辑:程序博客网 时间:2024/05/21 17:36
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;        }    }}
0 0
原创粉丝点击