Number Game
Time limit2sMemory limit1024 MB
Given distinct integers, players place chips on a table while some common difference d of at least 2 must fit all table values; list Petya's winning first moves.
- Level
Medium7 of 10
- Topics
- Game theory, Number theory, Bit manipulation
- Solved
- No attempts yet
Problem
An arithmetic progression with common difference is a sequence in which the difference between any two consecutive terms is . For example, 2, 5, 8, 11 is an arithmetic progression with difference 3. Petya and Vasya play a game with a basket of chips. Each chip has a different integer written on it: . The players take turns, and Petya moves first. On a turn, a player takes one chip from the basket and places it on the table. The player may choose any chip. Then the player must name an integer such that every number on the chips now on the table is a term of some arithmetic progression with difference . The terms do not have to be consecutive. For example, if the table holds chips 2, 8, and 11, the player can name . A player loses if they cannot move. That happens when the basket is empty, or when every chip left in the basket leads to a table with no valid . For example, if the basket holds 2, 3, 5, and 7, Petya can win by placing 3 first. Write a program that, given and the numbers , determines whether Petya can win no matter how Vasya plays, and finds every first move that leads to a win.
Input
The first line contains an integer (). The second line contains distinct integers (). Adjacent numbers are separated by exactly one space.
Output
The first line contains , the number of different first moves that let Petya win. If Vasya can win no matter what Petya does, print only 0. Otherwise, the second line contains the winning numbers, separated by exactly one space.
Hint
If the basket holds only 2 and 4, Petya places either chip, Vasya places the other one, and Petya has no chip left to place, so Petya loses. The first example is explained in the statement above.