poj2484
A Funny Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5584 | Accepted: 3493 |
Description
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
#include <iostream>
#include <stdio.h>
using namespace std;
//第一个人取完剩下3的倍数
int main()
{
int n;
while(~scanf("%d", &n))
{
if(n == 0)
break;
if(n < 3)
printf("Alice\n");
else
printf("Bob\n");
}
return 0;
}
poj2348
Euclid's Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9219 | Accepted: 3778 |
Description
Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):
an Stan wins.
25 7 11 7 4 7 4 3 1 3 1 0
an Stan wins.
Input
The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.
Output
For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.
Sample Input
34 12 15 24 0 0
Sample Output
Stan wins Ollie wins
//a获胜的可能性
//当前两个数的可以做大于一倍的差时候,a就可以控制
//x和y,例如x - ky ,当k >= 2时,假若a减去最大的k值
//a可以获胜,那么则a胜呗
//不胜,则不减去最大的k,剩一个,让b来面对输的情况
//当k == 1时,b就站在a的位置,决定自己的胜负、
//也就是判断谁先到差值k不为零的情况
//或者已经为倍数获胜的情况
#include <iostream>
#include <stdio.h>
using namespace std;
//a获胜的可能性
//当前两个数的可以做大于一倍的差时候,a就可以控制
//x和y,例如x - ky ,当k >= 2时,假若a减去最大的k值
//a可以获胜,那么则a胜呗
//不胜,则不减去最大的k,剩一个,让b来面对输的情况
//当k == 1时,b就站在a的位置,决定自己的胜负、
//也就是判断谁先到差值k不为零的情况
//或者已经为倍数获胜的情况
int main()
{
int x, y;
while(~scanf("%d%d", &x, &y))
{
if(x == 0 && y == 0)
break;
int k = 0;
while(x % y != 0 && y % x != 0)
{
if(x > y)
{
int f = x / y;
if(f > 1)
break;
else
{
k = (k + 1) % 2;
x = x - y;
}
}
else if(x < y)
{
int f = y / x;
if(f > 1)
break;
else
{
k = (k + 1) % 2;
y = y - x;
}
}
}
if(k == 0)
{
printf("Stan wins\n");
}
else
printf("Ollie wins\n");
}
return 0;
}