Official

C - 錬金術の対決 / Alchemy Showdown Editorial by admin Editorial for printing

gpt-5.5-high

Summary

No matter what order the adjacent materials are synthesized, the final remaining purity will always be \(A_1 \times A_2 \times \cdots \times A_N \bmod P\).
Therefore, the game’s tactics actually do not affect the result, and we only need to find the product of all elements modulo \(P\).

Discussion

In this problem, Takahashi wants to minimize the final value, while Aoki wants to maximize it.
At first glance, it seems that the result depends on which adjacent pair is chosen.

However, the synthesis operation is as follows:

  • Remove adjacent elements \(x\) and \(y\).
  • Replace them with \((x \times y) \bmod P\).

The crucial point here is that the product of the entire sequence modulo \(P\) remains invariant before and after the operation.

For example, suppose the sequence at some point is:

\[ B_1, B_2, \ldots, B_i, B_{i+1}, \ldots, B_m \]

and we synthesize \(B_i\) and \(B_{i+1}\).

The product of the entire sequence before synthesis is:

\[ B_1 \times B_2 \times \cdots \times B_i \times B_{i+1} \times \cdots \times B_m \]

After the synthesis, \((B_i \times B_{i+1}) \bmod P\) is inserted in place of \(B_i\) and \(B_{i+1}\).
Therefore, the product of the entire sequence after synthesis, when viewed modulo \(P\), is:

\[ B_1 \times \cdots \times (B_i \times B_{i+1}) \times \cdots \times B_m \]

which is the same as before the synthesis.

In other words, no matter which pair we choose,

\[ \text{product of the current sequence} \bmod P \]

remains invariant.

In the end, only one material remains. If we denote its value as \(V\), the product of the entire sequence is simply \(V\).
Therefore,

\[ V = A_1 \times A_2 \times \cdots \times A_N \bmod P \]

always holds.

Thus, no matter how both players act, the final value is the same, and there is no need to consider the optimal strategy.

For example, consider the case where \(P = 5\) and the sequence is \([2, 3, 4]\).

  • If we first synthesize \(2\) and \(3\), since \(2 \times 3 \bmod 5 = 1\), the sequence becomes \([1, 4]\).
    The final value is \(1 \times 4 \bmod 5 = 4\).
  • If we first synthesize \(3\) and \(4\), since \(3 \times 4 \bmod 5 = 2\), the sequence becomes \([2, 2]\).
    The final value is \(2 \times 2 \bmod 5 = 4\).

In both cases, this matches the product of the original sequence:

\[ 2 \times 3 \times 4 = 24 \equiv 4 \pmod{5} \]

If we naively search the game tree to “choose the best synthesis at each turn”, the number of choices is extremely large, and it will not run in time for \(N \leq 10^6\).
However, once we notice the invariant mentioned above, we can solve the problem by simply calculating the product.

Algorithm

  1. Initialize the answer to \(1\).
  2. For each \(A_i\), multiply the answer by \(A_i\) and take the modulo \(P\).
  3. Output the final answer.

That is, we compute:

\[ \left(\prod_{i=1}^{N} A_i\right) \bmod P \]

The same process works without any issues even when \(N = 1\).
The answer will be:

\[ 1 \times A_1 \bmod P = A_1 \]

Complexity

  • Time Complexity: \(O(N)\)
  • Space Complexity: \(O(1)\)

Key Points for Implementation

\(P\) is at most \(10^9+7\), and \(A_i\) is also at most \(P-1\).
Since the values can become large during multiplication, we should use long long instead of int.

At each step, if we do:

\[ ans = ans \times A_i \bmod P \]

then ans is always kept between \(0\) and \(P-1\) inclusive.

Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    long long P;
    cin >> N >> P;

    long long ans = 1 % P;
    for (int i = 0; i < N; i++) {
        long long A;
        cin >> A;
        ans = (ans * A) % P;
    }

    cout << ans << '\n';
    return 0;
}

This editorial was generated by gpt-5.5-high.

posted:
last update: