【数论】 POJ 2115 C Looooops

来源:互联网 发布:c语言输入保留两位小数 编辑:程序博客网 时间:2024/05/19 21:43

设一个X,则X%(1<<d)=B, X%C=A。。。然后用中国剩余定理求一下X就好了。。

有个小坑就是数据可能C=0.。。。

#include <iostream>  #include <queue>  #include <stack>  #include <map>  #include <set>  #include <bitset>  #include <cstdio>  #include <algorithm>  #include <cstring>  #include <climits>  #include <cstdlib>#include <cmath>#define maxn 5005#define eps 1e-10#define mod 1000000009#define INF 99999999  #define lowbit(x) (x&(-x))  #define lson o<<1, L, mid  #define rson o<<1 | 1, mid+1, R  typedef long long LL;//typedef int LL;using namespace std;LL a[maxn], b[maxn], n, ok;void extend_gcd(LL a, LL b, LL &d, LL &x, LL &y){    if(b == 0) { d = a, x = 1, y = 0; }    else { extend_gcd(b, a%b, d, y, x), y -= x*(a/b); }}void extend_chinese_reminder(LL &a1, LL &b1){LL x, y, g, tmp, i, a2, b2;for(i = 1; i < n; i++) {a2 = a[i], b2 = b[i];extend_gcd(a1, a2, g, x, y);if((b2-b1)%g) {ok = 1;return;}tmp = a2/g;x = x*(b2-b1)/g;x = (x%tmp+tmp)%tmp;b1 = a1*x+b1;a1 = (a1*a2)/g;b1 = (b1%a1+a1)%a1;}}int main(void){LL aa, a1, b1, ans, bb;while(scanf("%lld%lld%lld%lld", &b[0], &b[1], &a[0], &a[1]), a[0]!=0 || b[0]!=0 || a[1]!=0 || b[1]!=0) {if(a[0] == 0) {if(b[0] == b[1]) printf("0\n");else printf("FOREVER\n");continue;}aa = a[0], bb = b[0], a[1] = 1ll<<a[1];a1 = a[0], b1 = b[0], n = 2, ok = 0;extend_chinese_reminder(a1, b1);if(ok) {printf("FOREVER\n");continue;}b1 = ((b1-bb)%a1+a1)%a1;ans = b1/aa;printf("%lld\n", ans);}return 0;}


0 0