hdu 1729
Stone Game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1965 Accepted Submission(s): 539
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 to s stones if the size is s. 2. At the beginning of the game, there are some stones in these boxes. 3. The players take turns choosing a box and put a number of stones into the box. The number mustn’t be great than the square of the number 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 the size 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 whether the first player will win the game if both of the players make the best strategy.
1. There are n boxes; each box has its size. The box can hold up to s stones if the size is s. 2. At the beginning of the game, there are some stones in these boxes. 3. The players take turns choosing a box and put a number of stones into the box. The number mustn’t be great than the square of the number 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 the size 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 whether the first player will win the game if both of the players make the best 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 are ci stones in the box. N = 0 indicates the end of input and should not be processed.
Output
For each test case, output the number of the case on the first line, then output “Yes” (without quotes) on the next line if the first 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
/*
分析:
设当前的箱子容量为si,求出一个t满足:t + t * t < si,若是当前箱子里有ci颗石头,
1、ci > t 则必胜;
2、ci == t 则必败;
3、ci < t不可立即断定,将t作为si递归调用函数。
找出大于ci的最小必败点。
证明:
1.当ci<t时,(s,c)跟(t,c)的性质是一样的,(t,c)可以经过两个选手每人一个最优策略达到(s,c)状态,递归达到ci>t状态,这个过程正确性成立。
2.当ci>t时,先手一步就能达到胜利,等同于把前面的游戏规则(每一次放是数量都有限制,不能超过当前箱子内石子数的平方)忽略了。把若干个子游戏都转化成这种状态就类似于Nim游戏了,N堆石子每堆若干个,一次可以从任意一堆中取若干个。s=(a1,a2,a3,....,an),sg(s)=sg(a1)^sg(a2)^sg(a3)^...^sg(an)。
这题写了好久wrong answer,看了别人的解题报告后似懂非懂他们写的太有理论性了,自己写了下证明浅显易懂,适合跟我一类的菜鸟级水平的人看。
*/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int extend(int s,int c)
{
int t=(int)sqrt(s*1.0);
while(t*t+t>=s)
t--;
if(t<c)
return s-c;
else
return extend(t,c);
}
int main()
{
int n,i,c,s,Cases=1,ans;
while(scanf("%d",&n),n)
{
ans=0;
for(i=0;i<n;i++)
{
scanf("%d %d",&s,&c);
if(!s || !c || s==c)
continue;
ans^=extend(s,c);
}
printf("Case %d:\n",Cases++);
if(ans)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
#include<cstdio>
#include<cmath>
using namespace std;
int extend(int s,int c)
{
int t=(int)sqrt(s*1.0);
while(t*t+t>=s)
t--;
if(t<c)
return s-c;
else
return extend(t,c);
}
int main()
{
int n,i,c,s,Cases=1,ans;
while(scanf("%d",&n),n)
{
ans=0;
for(i=0;i<n;i++)
{
scanf("%d %d",&s,&c);
if(!s || !c || s==c)
continue;
ans^=extend(s,c);
}
printf("Case %d:\n",Cases++);
if(ans)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
浙公网安备 33010602011771号