Stone Game, Why are you always there?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 393    Accepted Submission(s):
132

Problem Description
“Alice and Bob are playing stone game...”
“Err....
Feel bored about the stone game? Don’t be so, because stone game changes all the
time!”
“What the hell are they thinking for?”
“You know, whenever Alice is
trying to make fun of Bob, she asked him to play stone game with him.”
“Poor
Bob... What’s the rule today?”
“It seems Alice only allows some fixed numbers
of continuous stones can be taken each time. And they begin with one string of
stones.”
“A string? Formed as a circle or a line?”
“A line.”
“Well, I
think I may help Bob with that.”
“How?”
“I may tell him to skip this round
if he has no chance to win.”
“Good idea maybe, I mean, Alice always let Bob
play first, because she think herself is smart enough to beat Bob no matter
how.”
“Yes, she’s actually right about herself. Let me see if Bob has a
chance to win...”
......
 
Input
There are multiple test cases, for each test
case:
The first line has a positive integer N (1<=N<=100).
The
second line contains N positive integers, a1, a2 ... an, separated by spaces,
which indicate the fixed numbers Alice gives.
The third line, a positive
integer M. (M<=1000)
Following M lines, one positive integer K
(K<=1000) each line. K means in this round, the length of the stone string.
 
Output
For each K, output “1” if Bob has a chance to win,
output “2” if Bob has no chance, or “0” if it’s undeterminable.
 
Sample Input
3
1 5 1
1
1
 
Sample Output
1
 
题目大意:给一个正整数集合,给一串连续在石头,每次只能取集合中元素个连续的石头,每次都是最优操作,判断是先手必胜还是后手必胜。
 
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=;
int sg[maxn],Set[],num; int mex(int n)
{
if(sg[n]!=-) return sg[n];
bool flag[maxn];
int i,j;
memset(flag,false,sizeof(flag));
for(i=;i<num && n>=Set[i];i++)
{
for(j=;j<=n-Set[i]+;j++)
{
sg[j-]=mex(j-);
sg[n-j-Set[i]+]=mex(n-j-Set[i]+);
flag[sg[j-]^sg[n-j-Set[i]+]]=true;
}
}
for(i=;i<maxn;i++)
if(!flag[i])
return sg[n]=i;
} int main()
{
int n,m,k,i;
while(~scanf("%d",&n))
{
memset(sg,-,sizeof(sg));
sg[]=;
for(i=;i<n;i++) scanf("%d",Set+i);
sort(Set,Set+n);num=;
for(i=;i<n;i++) if(Set[i]!=Set[num-]) Set[num++]=Set[i];//去重
scanf("%d",&m);
while(m--)
{
scanf("%d",&k);
if(sg[k]==-) sg[k]=mex(k);
if(sg[k]) puts("");
else puts("");
}
}
return ;
}
 

hdu 2999 sg函数(简单博弈)的更多相关文章

  1. bzoj 1188 [HNOI2007]分裂游戏(SG函数,博弈)

    1188: [HNOI2007]分裂游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 733  Solved: 451[Submit][Status ...

  2. hdu 1847(SG函数,巴什博弈)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. HDU 1848 Fibonacci again and again SG函数做博弈

    传送门 题意: 有三堆石子,双方轮流从某堆石子中去f个石子,直到不能取,问先手是否必胜,其中f为斐波那契数. 思路: 利用SG函数求解即可. /* * @Author: chenkexing * @D ...

  4. hdu 2147 SG函数打表(手写也可以) 找规律

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others) Total ...

  5. HDU-1848-Fibonacci again and again(SG函数,博弈)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意: 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样 ...

  6. HDU 1536 sg函数

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. hdu 1536 SG函数模板题

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. hdu 1848(SG函数)

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  9. gym/102059/problem/I. Game on Plane SG函数做博弈

    传送门: 题意: 给定一个正n边形的点.双方轮流连点成线,要求所画的线不能与之前的线相交.当某个人连成一个回路,这个人就输了.问先手必胜还是后手必胜. 思路: SG函数,因为一条线相当于把图劈成了两半 ...

随机推荐

  1. CodePlus #4 最短路

    题目传送门 北极为什么会有企鹅啊,而且北纬91°在哪啊? 关键在建图 因为任意两个城市间都可以互相到达,再加上还有"快捷通道",光是建图就已经\(\rm{T}\)了-- 但这题给了 ...

  2. js完成打印功能

    最近在做项目要求实现打印功能,我采用js方式来实现 window.print();会弹出打印对话框,打印的是window.docunemt.body.innerHTML中的内容,可以局部打印,也可以全 ...

  3. Manifest文件

    Manifest文件是简单的文本文件,它告知浏览器缓存的内容(或不缓存的内容) Manifest文件可以分为三个部分: 1.CAHCEMANIFEST-在此标题下列出的文件将在首次下载后进行缓存. C ...

  4. OI算法复习

    搜集一些算法,赛前背一背有好处的 转自各大网站 前排感谢:hzwer.风了咕凉 前辈...Orz 快速读入: int read() { ,f=;char ch=getchar(); ;ch=getch ...

  5. POJ-1961-Period(ZOJ-2177)

    这题是最短循环节,然后我们尝试小于字符串长度的所有长度,如果符合,我们就输出. 如果它等于0,说明它不循环,因为之前并没有重复的,如果i%(i-next[i])==0说明它是循环的,然后除一下得到周期 ...

  6. MySQL左右连接查询中的NULL的数据筛选问题

    这里使用左连接为例子,对于左连接是将左边表的数据显示,右边表中如果没有对应的数据则使用null填充. game表: game_type表: SELECT g.name,g.type_id,t.type ...

  7. sql 单表/多表查询去除重复记录

    单表distinct 多表group by group by 必须放在 order by 和 limit之前,不然会报错 *************************************** ...

  8. numpy 三个点的使用[...]

    numpy [...]语法简单使用 Python numpy中切片功能与列表切片类似,但功能更加强大 本文主讲numpy中[...]的简单使用,后续工作继续补充. import numpy >& ...

  9. python 列表加法"+"和"extend"的区别

    相同点 : "+"和"extend"都能将两个列表成员拼接到到一起 不同点 :   + : 生成的是一个新列表(id改变) extend : 是将一个列表的成员 ...

  10. ESP8266入门学习笔记1:资料获取

    乐鑫官网:https://www.espressif.com/zh-hans/products/hardware/esp8266ex/overview 乐鑫资料:https://www.espress ...