题目地址:点击打开链接
题意:
现在有n个箱子,每个箱子有一个容积si和一个当前箱子内的石子数ci,两个人轮流往这n个箱子里面放石子,每次放的个数为1-ci*ci。例如箱子里有3颗石子,那么下一个人就可以放1~9颗石子最后不能放入石子的人输。
一开始直接递推sg函数....数据量比较大 必然TLE...
思路:
我们知道s肯定是必败态,此时已经没有可以放入的石子了。
而c+1则是必胜点则有c+1+(c+1)*(c+1)>=s根据这两个公式我们可以求出小于s的最大必
败点t。 而t+1----s-1是必胜点因为(t+1---s-1都可以到达s这个点)
接下来分三种情况讨论:
当 c == t 时则此时一定是必败点直接返回0;
石子数: s s-1 s-2 s-3 ..... c .. .. t
对应的sg值:0 1 2 3 ..... s-c ... 0
而当 c < t 时此时将t当做s继续递归下去。。因为刚开是的s始终为必败点
参考博客:点击打开链接
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int solve(int s, int c)
{
int p = sqrt(s);
while(p+p*p >= s) p--;
if(c > p) return s-c;
else return solve(p, c);
}
int main(void)
{
int n, ca = 1;
while(cin >> n, n)
{
int ans = 0;
for(int i = 0; i < n; i++)
{
int s, c;
scanf("%d%d", &s, &c);
ans ^= solve(s, c);
}
printf("Case %d:\n", ca++);
puts(ans ? "Yes" : "No");
}
return 0;
}
Stone Game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3282 Accepted Submission(s): 1046
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.
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
Source
Recommend
lcy
本文介绍了一种两人轮流行棋的策略游戏——石子游戏。玩家需选择合适的石子数量放入不同容量的箱子中,目标是在对手无法继续放置时取胜。文章详细分析了必胜与必败状态的判断方法,并提供了具体的实现代码。
167

被折叠的 条评论
为什么被折叠?



