LA 5059 (找规律 SG函数) Playing With Stones
2015-03-09 18:05
302 查看
题意:
有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止。
判断先手是否必胜。
分析:
本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律。
经过一番“巧妙”的分析,有这样一个规律:
如果n是偶数,SG(n) = n / 2;
如果n是奇数,SG(n) = SG(n / 2);
这道题的意义不在于规律是什么,而是要自己能够写出求SG函数值的代码。顺便再体会一下mex(S)的含义。
AC
有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止。
判断先手是否必胜。
分析:
本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律。
经过一番“巧妙”的分析,有这样一个规律:
如果n是偶数,SG(n) = n / 2;
如果n是奇数,SG(n) = SG(n / 2);
这道题的意义不在于规律是什么,而是要自己能够写出求SG函数值的代码。顺便再体会一下mex(S)的含义。
#include <cstdio> long long SG(long long n) { if(n & 1) return SG(n >> 1); else return (n >> 1); } int main() { //freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while(T--) { int n; scanf("%d", &n); long long a, x = 0; while(n--) { scanf("%lld", &a); x ^= SG(a); } printf("%s\n", x ? "YES" : "NO"); } return 0; }
AC
相关文章推荐
- 【LA 5059】 Playing With Stones|博弈论|SG函数
- LA 5059 - Playing With Stones
- La 5059 - Playing With Stones
- UVALive 5059 C - Playing With Stones 博弈论Sg函数
- SG函数,找规律(石子游戏,LA 5059)
- Nim游戏的一个扩展——51nod 1661 黑板上的游戏+LA 5059 Playing With Stones
- UVALive 5059 C Playing With Stones
- UVaLive5059 Playing With Stones
- LA5059 Playing With Stones
- UESTC 1661 Playing With Stones 博弈打表
- UVALive 5059 Play with Stones SG函数,难度较低
- LA5059 Playing With Stones
- UVALive/LA 5059 Play with Stones 组合游戏/SG定理
- uva 1482 - Playing With Stones
- 【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones
- UVA-1482 Playing With Stones(SG函数打表找规律)
- UVA1482:Playing With Stones(SG)
- LA5059 Playing With Stones
- UVA - 1482 Playing With Stones
- uvalive 5059 Playing With Stones(组合游戏,SG函数)@