【数论】 POJ 2115 C Looooops

设一个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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值