题目链接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
#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;
}