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
    评论
中国剩余定理(Chinese Remainder Theorem)是一种求解同余方程组的方法,可以用于解决一些数学和计算机科学问题。 下面是使用 C++ 实现中国剩余定理的示例代码: ```c++ #include <iostream> using namespace std; // 求 a 和 b 的最大公约数 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } // 求 a 和 b 的最小公倍数 int lcm(int a, int b) { return a * b / gcd(a, b); } // 求逆元 int inv(int a, int m) { int x, y; int d = gcd(a, m); if (d == 1) { // 如果 a 和 m 互质,则存在逆元 x = 1; y = 0; while (a > 0) { int q = m / a; int t = m % a; m = a; a = t; int tmp = x - q * y; x = y; y = tmp; } return x < 0 ? x + m : x; } else { // 如果 a 和 m 不互质,则不存在逆元 return -1; } } // 中国剩余定理 int CRT(int a[], int m[], int n) { int M = 1; for (int i = 0; i < n; i++) { M = lcm(M, m[i]); // 求所有模数的最小公倍数 } int x = 0; for (int i = 0; i < n; i++) { int Mi = M / m[i]; int ti = inv(Mi, m[i]); x = (x + a[i] * Mi * ti) % M; } return x; } int main() { int a[] = {2, 3, 2}; int m[] = {3, 5, 7}; int n = 3; int x = CRT(a, m, n); // 求解同余方程组 cout << "x = " << x << endl; return 0; } ``` 在上面的代码中,`CRT()` 函数接收三个参数:`a` 数组表示模方程组中的余数,`m` 数组表示模数,`n` 表示模方程组的个数。函数返回值是模方程组的解。 需要注意的是,该代码中求解逆元的方法是使用扩展欧几里得算法。如果模数很大,可以使用欧拉定理和扩展欧拉定理来求解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值