MCOINS - Coins Game

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. 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. 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. And in all other cases Boyan being careful can win, no matter how Asen plays.

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.


The input describes m games.

The first line of the standard input contains the integers K, L and m, 1 < K < L < 10, 3 < m < 50. The second line contains m integers N1, N2, …, Nm, 1 ≤ Ni ≤ 1 000 000, i = 1, 2, …., m, representing the number of coins in each of the m towers

2 3 5
3 12 113 25714 88888


The standard output contains a string of length m composed of letters A and B. If Asen wins the ith game (no matter how the opponent plays), the ith letter of the string has to be A. When Boyan wins the ith game (no matter how Asen plays), the ith letter of the string has to be B.


Problem for kid - Please, think like kid.

hide comments
sandeepd: 2019-12-01 17:19:33

Nice problem, but output format cost me so many WAs :(
To anyone else facing the same - just use printf("%c") and no additional new line.

jackknowsit: 2019-10-30 18:56:28

Stop this AC comments. It's ridiculous. Nobody finds it useful.

adist98: 2019-07-24 19:25:00


amannegi227: 2019-07-22 05:36:27

Easy Nice One :AC

aj_254: 2019-06-12 10:31:51

solvable in pypy .just optimize your code to last extent.

harry_shit: 2019-06-03 12:06:33

damnn!!! , that was beautifully easy.

Rahul Kathuria: 2019-03-21 05:00:12

damn easy dp :P

exesharkx: 2019-01-24 14:32:01

AC in one go

manishjoshi394: 2018-11-25 06:56:44

If you can't figure out what to do, try reading Topcoder tutorial on 'Algorithm games'. Here is the link,

manishjoshi394: 2018-11-25 06:55:46

Even my memoization solution passed easily in first go. If you are getting RTE with Memoization, check if you are handling function calls with negative and zero values.

Added by:~!(*(@*!@^&
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:BOI For Kid 08