首页 > 代码库 > hdu 2999 sg函数(简单博弈)
hdu 2999 sg函数(简单博弈)
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...”
......
“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.
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
题目大意:给一个正整数集合,给一串连续在石头,每次只能取集合中元素个连续的石头,每次都是最优操作,判断是先手必胜还是后手必胜。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn=1010; 8 int sg[maxn],Set[110],num; 9 10 int mex(int n) 11 { 12 if(sg[n]!=-1) return sg[n]; 13 bool flag[maxn]; 14 int i,j; 15 memset(flag,false,sizeof(flag)); 16 for(i=0;i<num && n>=Set[i];i++) 17 { 18 for(j=1;j<=n-Set[i]+1;j++) 19 { 20 sg[j-1]=mex(j-1); 21 sg[n-j-Set[i]+1]=mex(n-j-Set[i]+1); 22 flag[sg[j-1]^sg[n-j-Set[i]+1]]=true; 23 } 24 } 25 for(i=0;i<maxn;i++) 26 if(!flag[i]) 27 return sg[n]=i; 28 } 29 30 int main() 31 { 32 int n,m,k,i; 33 while(~scanf("%d",&n)) 34 { 35 memset(sg,-1,sizeof(sg)); 36 sg[0]=0; 37 for(i=0;i<n;i++) scanf("%d",Set+i); 38 sort(Set,Set+n);num=1; 39 for(i=1;i<n;i++) if(Set[i]!=Set[num-1]) Set[num++]=Set[i];//去重 40 scanf("%d",&m); 41 while(m--) 42 { 43 scanf("%d",&k); 44 if(sg[k]==-1) sg[k]=mex(k); 45 if(sg[k]) puts("1"); 46 else puts("2"); 47 } 48 } 49 return 0; 50 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。