Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
1a#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef __int64 LL; int k; LL a, b, c; LL mod; LL gcd(LL a, LL b){ if(!b) return a; return gcd(b, a % b); } void egcd(LL a, LL b, LL& x, LL& y, LL &d1){ if(!b){ d1 = a; x = 1, y = 0; return; } egcd(b, a % b, x, y, d1); LL t = x; x = y; y = t - a / b * y; } void solve(){ LL a1 = c, b1 = mod, d1, x, y; egcd(c, mod, x, y, d1); if((b - a) % d1 != 0){ puts("FOREVER"); return; } x *= (b - a) / d1, y *= (b - a) / d1; d1 = b1 / d1; if(x < 0) x += ((-x) / d1 + 1) * d1; if(x > 0) x -= (x / d1) * d1; while(x < 0) x += d1; while(x >= d1) x -= d1; printf("%I64d\n", x); } int main(){ //freopen("in.txt", "r", stdin); while(~scanf("%d%I64d%I64d%I64d", &a, &b, &c, &k) && k){ mod = (LL)1 << k; solve(); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator