【poj2960】 S-Nim
来源:互联网 发布:seo 中文url 2017 编辑:程序博客网 时间:2024/06/11 18:56
http://poj.org/problem?id=2960 (题目链接)
题意:经典Nim游戏,只是给出了一个集合S,每次只能取S[i]个石子。
Solution
g(x)=mex{SG(x-s[1]),SG(x-s[2])……}
数据范围很小,可以暴力求SG,顺便记忆化一下。不知道为什么用map就TLE了。。。只好开数组了。
代码:
// poj2960#include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<map>#define inf 2147483640#define LL long long#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);using namespace std;inline LL getint() { LL x=0,f=1;char ch=getchar(); while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f;}const int maxn=10010;int k,n,m,sg[maxn],s[maxn];void calsg(int x) { if (sg[x]!=-1) return; //map<int,bool> mark; bool mark[10010]; memset(mark,0,sizeof(mark)); int temp; for (int i=1;i<=k;i++) { temp=x-s[i]; if (temp>=0) { if (sg[temp]==-1) calsg(temp); mark[sg[temp]]=1; } } for (int i=0;;i++) if (!mark[i]) {sg[x]=i;return;}}int main() { while (scanf("%d",&k)!=EOF && k) { for (int i=1;i<=k;i++) scanf("%d",&s[i]); sg[0]=0; for (int i=1;i<=maxn;i++) sg[i]=-1; scanf("%d",&m); while (m--) { scanf("%d",&n); int ans=0; while (n--) { int x;scanf("%d",&x); if (sg[x]==-1) calsg(x); ans^=sg[x]; } if (ans) printf("W"); else printf("L"); } printf("\n"); } return 0;}
0 0
- 【poj2960】 S-Nim
- Poj2960 S-Nim
- POJ2960 S-Nim SG函数博弈
- [POJ2960]S-Nim(博弈SG函数)
- POJ2960 S-Nim(博弈论:sg函数)
- 【POJ2960】S-Nim SG函数 博弈 裸题模板题
- HDU1536 或者POJ2960 S-Nim 传说中的SG函数
- poj2960——S-Nim(SG函数)
- poj2960【Nim博弈,SG】
- ACM-SG函数之S-Nim——hdu1536 hdu1944 poj2960
- S-Nim
- S-Nim
- S-Nim
- HDOJ 1536 S-NIM
- POJ 2960 S-Nim
- hdu1536 S-Nim(博弈)
- POJ 2960 S-Nim
- hdu 1536 S-Nim
- 网络分析
- Scrapy实战之抓取豆瓣图书
- i2c设备驱动实例 ds1307为例
- Android UI高级控件中的ListView
- 【ZZULI】-1998-985的数字难题(思维)
- 【poj2960】 S-Nim
- ROS(一)工程搭建
- POJ 1094 Sorting It All Out
- linux 内存查看方法:meminfo\maps\smaps\status 文件解析
- ubuntu14.04下面安装himawaripy模块时候报下面这个错误
- 安装好Ubuntu16.04后要做的事
- POJ3268
- 电子-电路-电磁(一)阻抗匹配
- 【POJ 2485 Highways】