码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2115

时间:2015-08-05 12:01:32      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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;
        }
    }
}

POJ 2115

标签:

原文地址:http://www.cnblogs.com/aoxuets/p/4704056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!