/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君と青木君は錬金術師で、素材の合成についての勝負をしています。
作業台の上に N 個の素材が一列に並んでおり、i 番目の素材には純度を表す 0 以上 P - 1 以下の整数 A_i が記されています。ここで P は素数です。
二人は第 1 ターンから第 N - 1 ターンまで、合計 N - 1 回の操作を順に行います。奇数回目のターンは高橋君が、偶数回目のターンは青木君が操作します。各ターンでは、操作を行うプレイヤーは必ず以下の操作を 1 回行います。
- 現在の列の中で隣り合っている 2 つの素材を 1 組選ぶ。選んだ 2 つの素材の純度をそれぞれ x, y とするとき、それら 2 つの素材を列から取り除き、代わりにそれらが連続して並んでいた位置に純度 (x \times y) \bmod P(すなわち x \times y を P で割った余り)の新しい素材 1 つを挿入する。
この操作により、他の素材の相対的な並び順は変化せず、列に含まれる素材の個数は 1 つ減ります。
N - 1 回の操作の後、素材はちょうど 1 つだけ残ります。最終的に残った素材の純度を V とします。
高橋君は V をできるだけ 小さく したいと考えており、青木君は V をできるだけ 大きく したいと考えています。
両者が最適に行動したとき、最終的に残る素材の純度を求めてください。
なお、N = 1 の場合は操作を行う必要がなく、最初から残っている唯一の素材の純度がそのまま答えとなります。
制約
- 1 \leq N \leq 10^6
- 2 \leq P \leq 10^9 + 7
- P は素数である
- 0 \leq A_i \leq P - 1
- 入力はすべて整数である
入力
N P A_1 A_2 \ldots A_N
- 1 行目には、素材の個数を表す整数 N と、素数 P が、スペース区切りで与えられる。
- 2 行目には、各素材の純度を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。各 A_i は 0 以上 P - 1 以下である。
出力
両者が最適に行動したときの、最終的に残る素材の純度(0 以上 P - 1 以下の整数)を 1 行で出力せよ。
入力例 1
3 7 2 3 4
出力例 1
3
入力例 2
4 5 1 0 3 4
出力例 2
0
入力例 3
12 101 17 42 0 8 99 23 56 74 31 65 12 90
出力例 3
0
入力例 4
30 1000000007 123456789 987654321 314159265 271828182 0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393
出力例 4
0
入力例 5
1 2 0
出力例 5
0
Score : 333 pts
Problem Statement
Takahashi and Aoki are alchemists competing in a game of synthesizing materials.
There are N materials lined up in a row on a workbench. The i-th material has an integer A_i (0 \leq A_i \leq P - 1) written on it, representing its purity. Here, P is a prime number.
The two players perform a total of N - 1 operations in order, from Turn 1 to Turn N - 1. Takahashi takes the odd-numbered turns, and Aoki takes the even-numbered turns. On each turn, the player taking the turn must perform the following operation exactly once:
- Choose a pair of adjacent materials in the current row. Let x and y be the purities of the chosen materials. Remove these two materials from the row, and insert a single new material with purity (x \times y) \bmod P (the remainder when x \times y is divided by P) at the position where they were consecutively located.
This operation does not change the relative order of the other materials, and the number of materials in the row decreases by 1.
After N - 1 operations, exactly one material will remain. Let V be the purity of the final remaining material.
Takahashi wants to make V as small as possible, while Aoki wants to make V as large as possible.
Find the purity of the final remaining material when both players play optimally.
Note that if N = 1, no operations are performed, and the purity of the only material initially present is the answer.
Constraints
- 1 \leq N \leq 10^6
- 2 \leq P \leq 10^9 + 7
- P is a prime number.
- 0 \leq A_i \leq P - 1
- All input values are integers.
Input
N P A_1 A_2 \ldots A_N
- The first line contains the integer N, representing the number of materials, and the prime number P, separated by a space.
- The second line contains the integers A_1, A_2, \ldots, A_N, representing the purity of each material, separated by spaces. Each A_i satisfies 0 \leq A_i \leq P - 1.
Output
Print the purity of the final remaining material (an integer between 0 and P - 1, inclusive) when both players play optimally in a single line.
Sample Input 1
3 7 2 3 4
Sample Output 1
3
Sample Input 2
4 5 1 0 3 4
Sample Output 2
0
Sample Input 3
12 101 17 42 0 8 99 23 56 74 31 65 12 90
Sample Output 3
0
Sample Input 4
30 1000000007 123456789 987654321 314159265 271828182 0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393
Sample Output 4
0
Sample Input 5
1 2 0
Sample Output 5
0