博弈之sg函数
来源:互联网 发布:翻车鱼能吃吗 知乎 编辑:程序博客网 时间:2024/06/05 09:49
Stone Game
Time Limit : 5000/1000ms(Java/Other) MemoryLimit : 65535/32768K (Java/Other)
Total Submission(s) :12 AcceptedSubmission(s) : 6
Font: Times NewRoman | Verdana | Georgia
Font Size: ← →
Problem Description
This game is a two-player game and is played as follows:
1. There are n boxes; each box has its size. The box can hold up tos stones if the size is s.
2. At the beginning of the game, there are some stones in theseboxes.
3. The players take turns choosing a box and put a number of stonesinto the box. The number mustn’t be great than the square of thenumber of stones before the player adds the stones. For example,the player can add 1 to 9 stones if there are 3 stones in the box.Of course, the total number of stones mustn’t be great than thesize of the box.
4.Who can’t add stones any more will loss the game.
Give an Initial state of the game. You are supposed to find whetherthe first player will win the game if both of the players make thebest strategy.
1. There are n boxes; each box has its size. The box can hold up tos stones if the size is s.
2. At the beginning of the game, there are some stones in theseboxes.
3. The players take turns choosing a box and put a number of stonesinto the box. The number mustn’t be great than the square of thenumber of stones before the player adds the stones. For example,the player can add 1 to 9 stones if there are 3 stones in the box.Of course, the total number of stones mustn’t be great than thesize of the box.
4.Who can’t add stones any more will loss the game.
Give an Initial state of the game. You are supposed to find whetherthe first player will win the game if both of the players make thebest strategy.
Input
The input file contains several test cases.
Each test case begins with an integer N, 0 < N ≤ 50,the number of the boxes.
In the next N line there are two integer si, ci (0 ≤ ci ≤ si ≤1,000,000) on each line, as the size of the box is si and there areci stones in the box.
N = 0 indicates the end of input and should not beprocessed.
Each test case begins with an integer N, 0 < N ≤ 50,the number of the boxes.
In the next N line there are two integer si, ci (0 ≤ ci ≤ si ≤1,000,000) on each line, as the size of the box is si and there areci stones in the box.
N = 0 indicates the end of input and should not beprocessed.
Output
For each test case, output the number of the case on the firstline, then output “Yes” (without quotes) on the next line if thefirst player can win the game, otherwise output “No”.
Sample Input
3
2 0
3 3
6 2
2
6 3
6 3
0
Sample Output
Case 1:
Yes
Case 2:
No
这个sg郁闷了我好久,请教了一些人,让我百度一下,还是自己看吧。。。。。。
首先我们假设c 是必败点,则有c+c*c<s(即使放了最大的c*c也不能放满)
而c+1则是必胜点则有c+1+(c+1)*(c+1)>=s根据这两个公式我们可以求出小于s的最大必败点t。
而t+1----s-1是必胜点因为(t+1---s-1都可以到达s这个点)。。。。(原理1,,只要能够到达必败点则一定是必胜点,,2,,每一步只能到达必胜点则这点一定是必败点)
接下来分三种情况讨论:
当 c == t 时则此时一定是必败点直接返回0;
当c > t 时 此时C是必胜点只要返回C的后继集合中的最小即可。。。而此时最小是s-c
s s-1 s-2 s-3 ..... c .. .. t t-1 t-2 t-3 .................... 1
0 1 2 3 ..... s-c ... 0
而当 c < t 时此时将t当做s继续递归下去。。应为刚开是的s始终为必败点。。。
我的代码:
#include<stdio.h>
#include<math.h>
int getsg(int s,int c)
{
int t;
t=(int)sqrt(s);
while(t*t+t>=s)
{
t--;
}
if(c>t)
return s-c;
else if(c==t)
return 0;
else
return getsg(t,c);
}
int main()
{
int t,x,y,count=1,c,i;
while(scanf("%d",&t),t)
{
c=0;
for(i=0;i<t;i++)
{
scanf("%d%d",&x,&y);
c^=getsg(x,y);
}
printf("Case %d:\n",count++);
if(c)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
- 博弈之sg函数
- 博弈之SG函数
- 博弈问题之SG函数博弈小结
- (转载)博弈之SG函数
- 博弈之Nim游戏和sg函数
- 博弈之sg函数(模板)
- 博弈算法 之 SG 函数的运用
- 博弈之 Nim 游戏和 sg 函数
- SG函数 博弈
- 博弈のSG函数
- hoj1848 Sg函数博弈
- 博弈sg函数
- 博弈SG函数
- 博弈sg函数
- 博弈通解:SG函数
- 博弈 SG函数
- sg函数 和 博弈
- 博弈sg函数模板
- 2011年10月07日
- (巴什博弈)悼念512汶川大地震遇难…
- 悬浮窗添加SurfaceView
- 2516取石子游戏(简单博弈)
- ACM大牛!!!!!!
- 博弈之sg函数
- 博弈之sg函数 A chess game
- hdu 邂逅明下(Bash博弈的变形)
- 2011年10月09日
- hdu 2509 Be the Winner(博弈Nim)
- hdu 1907 John
- 杭电46道DP牛人总结
- hdu 2084 数塔
- JS字符串转日期