The Seventh Hunan Collegiate Programming Contest box game, hunancollegiate
- [1107] box games
- Time Limit: 1000 MS memory limit: 65535 K
- Problem description
- There are two identical boxes, one with n balls and the other with one ball. Alice and Bob invented a game. The rule is as follows: Alice and Bob take turns to operate. When Alice first operates each operation, the gamer first checks which box contains fewer balls, then empty the box (the ball in the box is directly thrown away), and then take some balls from the other box into the box so that both boxes have at least one ball. If a player cannot perform operations, it will lose. Is a typical game:
Bob cannot continue to operate on two boxes with one ball each, so Alice wins. Your task is to find out who will win. Assuming that both of them are smart, they always adopt the optimal strategy.
- Input
- The input can contain up to 300 groups of test data. Each group contains only one row of data, which contains an integer n (2 <=n <= 10 ^ 9 ). The input end flag is n = 0.
- Output
- For each group of data, the name of the winner is output.
- Sample Input
2 3 4 0
- Sample output
Alice Bob Alice
- Prompt
None
- Source
The Seventh Hunan Collegiate Programming Contest
At the beginning of this question, I thought about how to calculate 1.1. Finally, I decided whether it was an odd or even number based on the number of required steps. If the odd number is alice's win, the even number is bob's win. Later I found that this idea was not feasible, later I changed my mind. Because an implicit condition of the question was not given, I mistakenly thought that as long as the number of two boxes is equal, I could not be divided, in fact, this idea is also wrong. It won't be able to be split until the two boxes are both 1, and we can deduce that when n = 2Bob wins at n-1;
Then we can quickly write the code:
#include <cstdio>#include <cstring>using namespace std;int main(){ int n; while(scanf("%d",&n)&&n) { n++; while(n%2==0) { n=n/2; } if(n==1) printf("Bob\n"); else printf("Alice\n"); } return 0;}
While (n & 1) = 0) // determine whether the Division can be 2 n> = 1; // divide 2