2019徐州网络A(中国剩余定理非互质m+判断斐波那契数)

题目链接https://nanti.jisuanke.com/t/41383 Who is better?
题目分两部分,先找出最小的n,在判断n经过博弈决定谁赢
找出n的过程是中国剩余定理的变种,未给m互质的条件,证明过程https://www.iteye.com/blog/yzmduncan-1323599
板子:

void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
	if (!b) {
		d = a, x = 1, y = 0;
	}
	else {
		gcd(b, a % b, d, y, x);
		y -= x * (a / b);
	}
}
LL crt(LL *m, LL *r, int n) {
	LL M = m[1], R = r[1], x, y, d;
	for (int i = 2; i <= n; ++i) {
		gcd(M, m[i], d, x, y);
		if ((r[i] - R) % d)
			return -1;
		x = (r[i] - R) / d * x % (m[i] / d);
		R += x * M;
		M = M / d * m[i];
		R %= M;
	}
	return R > 0 ? R : R + M;
}

不存在即返回-1
博弈时若n为斐波那契数则后手赢,否则前手赢,可用数学归纳法证明。
斐波那契判断暴力循环即可,因为在循环100次就已经超过ll了

#include <bits/stdc++.h>
#define N 1025
#define LL long long
const LL mod=1000000007;
const double pi = acos(-1.0);
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
int n;
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
	if (!b) {
		d = a, x = 1, y = 0;
	}
	else {
		gcd(b, a % b, d, y, x);
		y -= x * (a / b);
	}
}
LL crt(LL *m, LL *r, int n) {
	LL M = m[1], R = r[1], x, y, d;
	for (int i = 2; i <= n; ++i) {
		gcd(M, m[i], d, x, y);
		if ((r[i] - R) % d)
			return -1;
		x = (r[i] - R) / d * x % (m[i] / d);
		R += x * M;
		M = M / d * m[i];
		R %= M;
	}
	return R > 0 ? R : R + M;
}
LL m[maxn], r[maxn];
int main() {

	cin >> n;
		for (int i = 1; i <= n; ++i)
			cin>>m[i]>>r[i];
		LL x = crt(m, r, n),y=1;
		if (x > 1e15||x<=0)
			cout << "Tankernb!";
		else
		{
			for (LL i = 1, j = 1; i <= 1e15;)
			{
				y = i + j;
				j = i;
				i = y;
				if (y >= x)
					break;
			}
			if (y == x)
				cout << "Lbnb!";
			else cout << "Zgxnb!";
		}
	//system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值