#P5379. [THUPC 2019] 令人难以忘记的题目名称

[THUPC 2019] 令人难以忘记的题目名称

Description

Now there is an integer sequence SS of length NN (indexed from 00). Alice and Bob play a game on this sequence.

The game proceeds in rounds. In each round:

  • Alice provides a positive integer sequence TT of length NN.
  • Bob sees the TT given by Alice, then chooses an integer xx in [0,N1][0, N-1].
  • Then we transform SS into SS' by the following rule:
Si=Si+T(i+x)modN{S'}_{i} = S_{i} + T_{(i+x)\bmod N}
  • Take SS' as the new SS, and this round ends.

If after some round ends, every number in SS is a multiple of a given prime PP, then Alice wins.

Given NN and the initial sequence SS, determine whether Alice can guarantee a win in finitely many steps. If yes, what is the minimum number of rounds in which she can guarantee a win.

Input Format

The first line contains two non-negative integers N,PN, P, and PP is guaranteed to be a prime.

The next line contains NN integers separated by spaces, describing the initial sequence SS (0Si1090\le S_i \le 10^9).

It is guaranteed that N3×105N\le 3\times 10^5 and P200P\le 200.

Output Format

Output one integer. If Alice cannot guarantee a win in finitely many steps, output 1-1. Otherwise, output an integer xx indicating the minimum number of rounds in which Alice can win.

4 2
0 1 0 1
2

Hint

Sample Explanation

One possible game process is:

  • Round 1: T=[1,0,1,0]T=[1, 0, 1, 0], x=0x=0, and the transformed sequence is S=[1,1,1,1]S'=[1,1,1,1].
  • Round 2: T=[1,1,1,1]T=[1,1,1,1]. No matter what xx is, the transformed sequence is S=[2,2,2,2]S'=[2,2,2,2].

It can be proved that 22 rounds is optimal.

From THUPC (THU Programming Contest, Tsinghua University Programming Contest) 2019.

Resources such as solutions can be found at https://github.com/wangyurzee7/THUPC2019.

Translated by ChatGPT 5