n个瓶子,每个瓶子里面有一些石头,每次向瓶子里面放石头的数量不能超过瓶子中已经存在的石头数量的平方。
很容易看出是“组合游戏和”,因此只需要求出每个瓶子的sg函数值,然后求Nim和即可。
寻找必败态:
设t,t*t +t < s 而且使 t 尽量的大,则(t+1)*(t+1) +(t+1) >= s,因此
1. c > t 则当前状态是必胜态,因为c*c+c >= s成立
2. c == t 则当前状态为必败态,因为最多放c*c个石头,瓶子未满,对手必胜,至少放1个石头,则对手也是必胜。
3. c < t 当前状态无法确定,而在瓶子中已经有c个石头的前提下,容量为 s 和容量为 t 的状态是等价的,如果(t, c)是必败态,则(s, c)也是必败态。
用s-c作为返回的sg函数,参考:http://www.cnblogs.com/vongang/archive/2011/09/27/2193375.html
#include <cstdio> #include <cmath> int SG(int s, int c) { int t = sqrt(s); while (t*t + t >= s) t--; if (c > t) return s-c; else return SG(t, c); } int main() { int cas = 1, ans, c, s, n; while(scanf("%d", &n)==1 && n) { ans = 0; while(n--) { scanf("%d%d", &s, &c); ans ^= SG(s, c); } printf("Case %d:\n", cas++); puts(ans?"Yes":"No"); } return 0; }