SG函数 Alice and Bob
来源:互联网 发布:绝仁弃义 知乎 编辑:程序博客网 时间:2024/04/30 16:41
题目大意是给定n的石子堆,每次只能从一个堆中取石子,每次取得石子不能少于该堆的一半,为先手能不能赢
方法一 暴力DFS:
直接爆搜sg
#include <iostream>#include <cstring>#include <iostream>#include <cstdio>#define MEM(a,x) memset(a,x,sizeof(a))using namespace std;int gcd(int a,int b){ return b==0?a:gcd(b,a%b); }typedef long long llt ;const int maxn = 3000;int sg[maxn]={0};bool flag[maxn];int getSG(int depth){ if ( -1 != sg[depth] ) return sg[depth]; int l = (int)(depth/2.0+0.5); for (int i = depth ;i >= l;--i) flag[ getSG(depth-i)] = true; for (int i = 0;i < maxn;++i) if ( !flag[i] ) return i;}void init(){ fill(sg,sg+maxn,-1); sg[0] = 0; for (int i = 1;i < maxn;++i){ MEM(flag,0); sg[i] = getSG(i); }}int main(){ init(); int n; while(scanf("%d",&n)&&n){ int x; int ans = 0; for (int i = 0;i < n;++i){ scanf("%d",&x); ans ^= sg[x]; } if (ans)printf("YES\n"); else printf("NO\n"); } return 0;}
方法二找 sg的规律
0 1 22 3333 44444444
2的n次方递增
const int maxn = 2050;int sg[maxn];void sieve(){ sg[0] = 0; sg[1] = 1; int temp = 2; for (int i = 2; i < maxn;++i){ int j = 0; for (;j+i < maxn && j < (1<<(temp-1));++j) sg[i+j] = temp; temp++; i += j - 1; }}int main(){ sieve(); int n; while(scanf("%d",&n)&&n){ int x; int ans = 0; for (int i = 0;i < n;++i){ scanf("%d",&x); ans ^= sg[x]; } if (ans)printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- SG函数 Alice and Bob
- Alice and Bob (SG函数)
- zoj 3666 Alice and Bob , SG函数
- Alice and Bob 题解 SG函数
- ZOJ 3666 Alice and Bob(博弈 sg 函数入门)
- Acdream 1112 Alice and Bob(sg函数)
- hdu 4111 Alice and Bob 博弈论 sg函数
- ACdream 1112 Alice and Bob (SG函数)
- hdu4111 Alice and Bob---sg dp记忆化求sg
- ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)
- ACdream 1112 Alice and Bob (SG函数+线性素数筛)
- ZOJ Problem Set - 3666 Alice and Bob(SG)
- Alice and Bob
- 2683. Alice and Bob
- 1798. Alice and Bob
- hdu4268 Alice and Bob
- HDU4268 Alice and Bob
- Alice, Bob and Chocolate
- 线段树(2)
- JS传递参数
- Shader Scale
- 解决Url带中文参数乱码问题
- java中保留几位小数
- SG函数 Alice and Bob
- 安卓逆向学习第一天
- 定时任务 中的job中注入的service接口 为空 解决方法
- hdu 4301 Divide Chocolate (dp )
- hdu2063 过山车--最大匹配数 & 匈牙利
- 5-26 Windows消息队列
- Retrofit2简单使用初试用
- MySQL的存储过程
- Material Design系列,自定义Behavior之上滑显示返回顶部按钮