对称博弈
对称博弈
Coin Game (HDU - 3951)
After hh has learned how to play Nim game, he begins to try another coin game which seems much easier.
The game goes like this:
Two players start the game with a circle of n coins.
They take coins from the circle in turn and every time they could take 1~K continuous coins.
(imagining that ten coins numbered from 1 to 10 and K equal to 3, since 1 and 10 are continuous, you could take away the continuous 10 , 1 , 2 , but if 2 was taken away, you couldn't take 1, 3, 4, because 1 and 3 aren't continuous)
The player who takes the last coin wins the game.
Suppose that those two players always take the best moves and never make mistakes.
Your job is to find out who will definitely win the game.
The game goes like this:
Two players start the game with a circle of n coins.
They take coins from the circle in turn and every time they could take 1~K continuous coins.
(imagining that ten coins numbered from 1 to 10 and K equal to 3, since 1 and 10 are continuous, you could take away the continuous 10 , 1 , 2 , but if 2 was taken away, you couldn't take 1, 3, 4, because 1 and 3 aren't continuous)
The player who takes the last coin wins the game.
Suppose that those two players always take the best moves and never make mistakes.
Your job is to find out who will definitely win the game.
InputThe first line is a number T(1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers N(3<=N<=10 9,1<=K<=10).OutputFor each case, output the number of case and the winner "first" or "second".(as shown in the sample output)Sample Input
2
3 1
3 2
Sample Output
Case 1: first
Case 2: second
题意:给你n个硬币排成一圈,编号1-n,只能翻转连续的1~k个的硬币。翻最后一枚硬币者赢。
题解:如果k=1,那么n为奇数,先手必胜,n为偶数,后手必胜。
如果k!=1{
如果k >= n 即先手可以一次性取完,先手必胜;
如果k<n,那么后手必胜 因为先手取完第一次后,后手可以在取完剩下的或者将剩下的截成相同的两段,之后先手怎么取,后手怎么取,所以必胜。
}
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 int casen;
6 int cas;
7 int main()
8 {
9 int n,k;
10 scanf("%d",&casen);
11 while(casen--)
12 {
13
14 scanf("%d%d",&n,&k);
15 printf("Case %d: ",++cas);
16 if(n<=k)
17 printf("first\n");
18 else if(k==1)
19 {
20 if(n%2)
21 printf("first\n");
22 else
23 printf("second\n");
24 }
25 else
26 {
27 printf("second\n");
28 }
29 }
30 return 0;
31 }
同理:
A Funny Game
Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 <= n <= 10 6) coins in a circle, as Figure 1 shows. A move consists in removing one or two adjacent coins, leaving all other coins untouched. At least one coin must be removed. Players alternate moves with Alice starting. The player that removes the last coin wins. (The last player to move wins. If you can't move, you lose.)
Figure 1
Note: For n > 3, we use c1, c2, ..., cn to denote the coins clockwise and if Alice remove c2, then c1 and c3 are NOT adjacent! (Because there is an empty place between c1 and c3.)
Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.
Figure 1
Note: For n > 3, we use c1, c2, ..., cn to denote the coins clockwise and if Alice remove c2, then c1 and c3 are NOT adjacent! (Because there is an empty place between c1 and c3.)
Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.
Input
There are several test cases. Each test case has only one line, which contains a positive integer n (1 <= n <= 10 6). There are no blank lines between cases. A line with a single 0 terminates the input.
Output
For each test case, if Alice win the game,output "Alice", otherwise output "Bob".
Sample Input
1
2
3
0
Sample Output
Alice
Alice
Bob
题目的意思就是两个人轮流拿硬币,Alice先拿,Alice拿的时候可以选择拿走一个或者拿走相邻的两个,谁拿完最后的石子胜利。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
if(n==1||n==2)
{
puts("Alice");
continue;
}
else
{
puts("Bob");
}
}
}