|
| 1 | +Asen and Boyan are playing the following game. They choose two different positive integers K and L, and start the game with a tower of N coins. |
| 2 | +Asen always plays first, Boyan � second, after that � Asen again, then Boyan, and so on. The boy in turn can take 1, K or L coins from the tower. |
| 3 | +The winner is the boy, who takes the last coin (or coins). After a long, long playing, Asen realizes that there are cases in which he could win, no matter how Boyan plays. |
| 4 | +And in all other cases Boyan being careful can win, no matter how Asen plays. |
| 5 | + |
| 6 | +So, before the start of the game Asen is eager to know what game case they have. Write a program coins which help Asen to predict the game result for given K, L and N. |
| 7 | +------------------------------------------ |
| 8 | + |
| 9 | +Precompute the winners for all games and then answer each query in O(1) time. |
| 10 | + |
| 11 | +The player who cannot move on his turn loses. The winner is the player who has not lost. |
| 12 | + |
| 13 | +Winner(0) = 2 |
| 14 | + |
| 15 | +After the first player makes a move from i coins to i' coins, he is the second player in a game of i' coins. |
| 16 | + |
| 17 | +If Winner(i - 1) or Winner(i - k) or Winner(i - l) is winning for the second player, the first player wins. |
| 18 | +If they're all winning for the first player, the second player wins. |
| 19 | + |
| 20 | +-------------------------------------------------------------- |
| 21 | + |
| 22 | +void precompute(vector <int> &winner, int MAX_GAMES, int k, int l) |
| 23 | +{ |
| 24 | + winner[0] = 2; |
| 25 | + |
| 26 | + for(int i = 1; i < MAX_GAMES; i++) |
| 27 | + { |
| 28 | + if(winner[i - 1] == 2 || (legal_move(i - k) && winner[i - k] == 2) || (legal_move(i - l) && winner[i - l] == 2)) |
| 29 | + { |
| 30 | + winner[i] = 1; |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + winner[i] = 2; |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +int main() |
| 40 | +{ |
| 41 | + const int MAX_GAMES = 1e6 + 1; |
| 42 | + vector <int> winner(MAX_GAMES); |
| 43 | + int no_of_games, k, l; |
| 44 | + scanf("%d %d %d", &k, &l, &no_of_games); |
| 45 | + |
| 46 | + precompute(winner, MAX_GAMES, k, l); |
| 47 | + |
| 48 | + while(no_of_games--) |
| 49 | + { |
| 50 | + int no_of_coins; |
| 51 | + scanf("%d", &no_of_coins); |
| 52 | + printf("%c", winner[no_of_coins] == 1 ? 'A' : 'B'); |
| 53 | + } |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
0 commit comments