Problem - 15C - Codeforces
目录
Nim游戏:
1~n的异或和:
代码:
Nim游戏:
n个石头堆,谁最后没得取谁败
我用的异或思考法,对所有堆异或。开局异或和为0的败
最后全是0,异或完也是0.
//最后是0,这位就输了
//A选手让异或和为0,B选手必须动一下,而他做任何操作都会使异或和不为0
//这时A可以接着让异或和为0(可以留个最大的数,将所有小数都异或完后,将这个大数减到异或和即可)
1~n的异或和:
(from https://blog.51cto.com/u_14972364/4820991)
本题要x ~ x+m-1的异或和,相当于 (1 ~ x+m-1) 异或上 (1 ~ x-1) (前缀思想)。
代码:
记得开long long
void solve()
{int n; cin >> n;int sum = 0;int x, m;for (int i = 1; i <= n; i++){cin >> x >> m;m = x + m - 1;x--;if (x % 4 == 0){sum ^= x;}else if (x % 4 == 1){sum ^= 1;}else if (x % 4 == 2){sum ^= x + 1;}if (m % 4 == 0){sum ^= m;}else if (m % 4 == 1){sum ^= 1;}else if (m % 4 == 2){sum ^= m + 1;}}if (sum == 0){cout << "bolik" << endl;}elsecout << "tolik" << endl;
}
signed main()
{ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int t = 1;//cin >> t;while (t--){solve();}return 0;
}