Official

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

claude4.8opus-high

Summary

This problem asks us to find the purity of the single remaining material after repeatedly choosing two adjacent materials and replacing them with the “product of their purities (\(\bmod P\))”. In fact, regardless of the order of operations, the answer is always the product of the purities of all materials (\(\bmod P\)), so the game elements do not affect the outcome.

Analysis

Although this problem is framed as a competitive game where “Takahashi wants to minimize \(V\) and Aoki wants to maximize it,” the key point is that no matter who chooses which position, the final result does not change.

Let’s consider the essence of the operation. Each operation consists of “removing two adjacent purities \(x, y\) and inserting \((x \times y) \bmod P\)”. After performing \(N-1\) operations, the single remaining material will ultimately be the product of the purities of all materials.

The crucial point here is that multiplication satisfies the commutative and associative laws. For example, consider the case with three materials \(a, b, c\):

  • If we combine the left two first: \((a \times b) \to (a \times b) \times c = a \times b \times c\)
  • If we combine the right two first: \((b \times c) \to a \times (b \times c) = a \times b \times c\)

In either order, the result is the same: \(a \times b \times c\). This also holds true when taking modulo \(P\).

In other words, no matter which adjacent pair is chosen in what order, the final remaining purity will always be

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

Neither Takahashi nor Aoki can change the result, and their strategies have absolutely no effect on the answer.

A naive approach such as “searching all optimal moves in the game” would result in exponential time complexity, but thanks to the above observation, we only need to calculate a simple product.

Algorithm

We only need to calculate the product of all material purities modulo \(P\).

  1. Initialize a variable result representing the answer to \(1\).
  2. For each material purity \(A_i\), sequentially calculate result = result * A_i % P.
  3. The final result is the answer.

By taking \(\bmod P\) at each step of the multiplication, we prevent the value from becoming too large.

Note that if \(N = 1\), no operations are performed, but since result is initialized to \(1\) and multiplied by only one element, \(A_1 \bmod P\) is naturally output and handled correctly.

Complexity

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

Since we can update the product while reading the input one by one, there is no need to store all elements in an array.

Implementation Points

  • Beware of Overflow: Since \(P\) is at most around \(10^9\), \(A_i\) and result can also be up to around \(10^9\). Their product can reach about \(10^{18}\), which does not fit in a 32-bit integer. Make sure to use a 64-bit integer type (such as long long in C++) and take % P immediately after multiplying.

  • By taking % P after each multiplication, the value stays within \(P^2 \approx 10^{18}\), which safely fits within the range of a long long (up to approximately \(9.2 \times 10^{18}\)).

  • Since \(N\) can be as large as \(10^6\), it is recommended to use fast I/O methods such as scanf for reading the input.

    Source Code

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

int main(){
    int N;
    long long P;
    scanf("%d %lld", &N, &P);
    long long result = 1 % P; // P>=2 なので 1、ただし安全に
    for(int i = 0; i < N; i++){
        long long a;
        scanf("%lld", &a);
        result = (result * (a % P)) % P;
    }
    printf("%lld\n", result % P);
    return 0;
}

This editorial was generated by claude4.8opus-high.