公式

D - 回転舞台の操作記録 / Operation Log of the Rotating Stage 解説 by admin 印刷ページ

imported

Overview

In each step, we choose either the specified clockwise or counterclockwise operation, and count the number of operation records that end up pointing to the target mark. We use dynamic programming to keep track of the number of records for each current mark.

Analysis

Since there are two choices in each step, examining all possible records would take \(2^N\) possibilities. Even if we maintain the total amount of rotation as a state, it requires a large amount of memory because the rotation amounts in each step can be large.

On the other hand, the orientation of the stage depends only on the mark the arrow points to. Even if the number of full revolutions made so far differs, as long as the currently pointed mark is the same, choosing the same operations in the future will result in the same orientation. Therefore, we define the state by the number of processed steps and the current mark.

Let \(dp_i[r]\) be the number of operation records such that the arrow points to mark \(r\) after the first \(i\) operations. Since there is only one record with no operations, \(dp_0[0]=1\), and all other values are \(0\).

Let the rotation amounts for the \((i+1)\)-th operation be \(a=A_{i+1}\bmod M\) and \(b=B_{i+1}\bmod M\). Choosing the clockwise direction from mark \(r\) transitions to \((r+a)\bmod M\), and choosing counterclockwise transitions to \((r-b+M)\bmod M\). We add \(dp_i[r]\) to each of these two destinations. Since we take modulo \(M\) for \(a\) and \(b\) beforehand, the latter expression will not be negative.

Even if the two destinations are the same, we add it twice. This is because the problem distinguishes between the records of rotation directions; reaching the same mark via different directions results in distinct operation records. Also, to avoid using values that have already been updated in the same step, we prepare a separate array for the next step.

Correctness

We prove by induction on the number of steps \(i\) that the DP correctly represents the number of records as defined. For \(i=0\), it counts only the single empty record, which is correct.

Any operation record of length \(i+1\) uniquely decomposes into a record of the first \(i\) operations and the direction chosen for the last operation. If the initial part ends at mark \(r\), it transitions to the corresponding mark above according to the final direction. By the induction hypothesis, this initial part is counted exactly once in \(dp_i[r]\).

Conversely, by appending each of the two directions to each record counted in \(dp_i[r]\), we can form valid records of length \(i+1\). Since records differing in either the initial part or the final direction are distinct, there are no omissions or duplicates in this addition. Therefore, the array for the next step also satisfies the definition.

From the above, \(dp_N[X]\) after all operations is the answer. Since we only perform additions on the counts, taking the modulo at each update yields the true count modulo \(998244353\) at the end.

Algorithm

  1. Initialize an array dp of length \(M\) with \(0\), and set dp[0] to \(1\).
  2. For each step, read \(A_i\) and \(B_i\), and replace them with their remainders modulo \(M\).
  3. Initialize the array for the next step to all zeros. For all marks \(r\), add dp[r] to the destinations of the two directions. Take modulo \(998244353\) after each addition.
  4. Swap the two arrays and proceed to the next step.
  5. After processing all \(N\) steps, output dp[X].

Complexity

Let \(N\) be the number of operations, and \(M\) be the number of marks.

The time complexity is \(O(NM)\). In each step, we initialize \(M\) states and make two transitions from each state.

The space complexity is \(O(M)\). We store the count arrays for the current and next steps, and process the rotation amounts as they are read.

Implementation Notes

Both the value in the array before addition and the value being added are less than \(998244353\). Their sum is at most \(2\times998244353-2<2^{31}-1\), which fits within a signed \(32\)-bit integer. Immediately after the addition, if the result is greater than or equal to the modulus, subtract the modulus once.

When \(M=1\), both operations lead to the unique mark \(0\). However, since we distinguish between the two directions and add them separately in each step, it is correctly counted in that case as well.

Source Code

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, x;
    cin >> n >> m >> x;
    const int mod = 998244353;
    vector<int> dp(m, 0), next_dp(m, 0);
    dp[0] = 1;
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        a %= m;
        b %= m;
        fill(next_dp.begin(), next_dp.end(), 0);
        for (int r = 0; r < m; ++r) {
            int u = (r + a) % m;
            int v = (r - b + m) % m;
            next_dp[u] += dp[r];
            if (next_dp[u] >= mod) next_dp[u] -= mod;
            next_dp[v] += dp[r];
            if (next_dp[v] >= mod) next_dp[v] -= mod;
        }
        dp.swap(next_dp);
    }
    cout << dp[x] << '\n';
}

This editorial was generated by imported.

投稿日時:
最終更新: