原题链接
- 题意:博弈,(n) 个游戏,每个游戏有 (b_i) 堆石子,每个人只能拿 (a_i) 的指数倍的数量,然后不能行动的人输。
- 题解:一开始莽规律,很容易的发现是和奇偶性改变有关,然后看出来如果 (a_i) 是奇数,那么 (b_i) 奇数则先手必胜为 (sg_i) 为 (1),然后如果 (a_i) 是偶数,那么就乱找规律,其实应该看 (sg) 并且很明显是个 (nim) 游戏,所以应该异或每个游戏的 (sg) 值。
- 代码:
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e5+9;
int a[N], b[N];
int SG[N];
int sg[2020][2202];
void solve() {
int n;cin >> n;
memset(SG, 0, sizeof SG);
for (int i = 1; i <= n ;i ++) {
cin >> a[i] >> b[i];
int len =0;
if (a[i] == 1||a[i] > b[i]) {
len = b[i];
SG[i] = b[i] & 1;
continue;
}
ll d = a[i] + 1;
if (b[i] % d == b[i]) {
SG[i] = 2;
} else {
if (b[i] %d != 0) {
b[i] %= d;
SG[i] = b[i] & 1;
}
}
}
ll nim = 0;
for (int i = 1; i <= n; i ++) {
nim ^= SG[i];
}
if (nim)cout <<1 << endl;
else cout << 2 << endl;
}
int tem[900];
void gogo() {
for (int i = 6; i <= 500; i ++) {
sg[i][0] = 0;
cout << "i:" << i;
for (int j = 1;j <= 500;j ++) {
cout <<" j:" << j << endl;
memset(tem, 0, sizeof tem);
//cout << "j-1 " << j-1 << endl;
tem[sg[i][j-1]]=1;
for (int k = 1,d = i; d <= j; d *= d) {
tem[sg[i][j-d]] = 1;
}
for (int k = 0; k <= j; k ++) {
if (!tem[k]) {
sg[i][j] = k;break;
//cout << k << '
';break;
}
}
}
for (int j = 1; j <= 100; j ++) {
cout << j<<"= " << sg[i][j] << "
";
}cout << endl;
getchar();
}
}
int main() {
//ios::sync_with_stdio(0);
freopen("powers.in", "r", stdin);
// freopen("output.out","w", stdout);
//gogo();
int t=1;cin >> t;
while (t--) {
solve();
}
return 0;
}