NIM
Annoula and Toto are playing the classic game of Nim. The game is played as follows:
Starting with a certain number of marbles on the table, each player must remove 1, 2, or 3 marbles on their turn.
The winner is the one who takes the last marble.
Annoula always plays first.
Since both players play optimally, they already know that the outcome of the game is predetermined based on the initial number of marbles. As a result, they got bored with these rules and are now experimenting with different choices for the number of marbles that can be removed.
Help Annoula figure out which starting numbers of marbles guarantee that, if she plays optimally, she will win!
Important! If the remaining marbles are fewer than one of the available choices, Annoula and Totos cannot make that move.
Input Data (STDIN
)
The st line contains a positive integer , the number of choices the players have for removing marbles.
The nd line line contains integers, the choices () in ascending order.
The d line contains a positive integer , the number of queries for the initial number of marbles.
This is followed by () lines, each containing a number , the initial number of marbles.
Output Data (STDOUT
)
The output consists of lines, each with a single number, 0
or 1
, depending on whether Annoula can definitely win with the corresponding initial number of marbles.
Examples
1st
Input (STDIN)
3
1 2 3
5
2
4
5
8
10
Output (STDOUT)
1
0
1
0
1
Example Explanation:
The players have choices:
They can remove , , or marbles per turn.
Additionally, there are 5 queries:
- : Annoula can play the second move and win the game directly.
- : No matter what move Annoula makes, Totos can win.
- If Annoula removes marble, Totos can take the remaining .
- If she removes , Totos can remove the other .
- If she removes , Totos takes the last .
- : Annoula can only remove marble and leave Toto with , a situation in which Totos cannot win (as explained above). Hence, Annoula wins.
- : Similarly, Annoula cannot win starting from marbles.
- : Following the same logic, Annoula can win starting from marbles.
2nd
Input (STDIN)
3
1 3 4
3
2
5
7
Output (STDOUT)
0
1
0
Subtasks
- of the points: the values are the numbers ,
- of the points:
- of the points:
Comments