Official

D - 回転舞台の操作記録 / Operation Log of the Rotating Stage Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

In each operation, we choose to rotate either clockwise or counterclockwise, and we want to count the number of ways to reach the final position \(X\). If we take the case where we always rotate counterclockwise as the baseline, each operation can be rephrased as “adding either \(0\) or \(A_i+B_i\)”, which allows us to solve the problem using dynamic programming.

Analysis

Trying All Choices Is Too Slow

Since there are \(2\) choices for each operation, there are \(2^N\) possible sequences of choices in total. Trying all of them is impossible for \(N\leq 2000\).

On the other hand, there are only \(M\) possible marks that the pointer can point to. Sequences of operations that reach the same position will transition identically in subsequent operations, so instead of tracking each sequence individually, we can collectively manage the number of sequences that reach each position.

Based on this idea, we can use dynamic programming with an \(O(NM)\) time complexity, where the state is the position.

Using Counterclockwise as the Baseline

Taking the clockwise direction as positive, the displacement in the \(i\)-th operation is one of the following:

  • Clockwise: \(+A_i\)
  • Counterclockwise: \(-B_i\)

Here, we can rewrite the clockwise displacement as:

\(A_i=-B_i+(A_i+B_i)\)

In other words, we can view both choices as having a common base displacement of \(-B_i\), and then we choose the following additional displacement:

  • Choose counterclockwise: additional displacement of \(0\)
  • Choose clockwise: additional displacement of \(A_i+B_i\)

For example, if \(A_i=2,\ B_i=1\), setting the common displacement to \(-1\) means no addition for counterclockwise and an addition of \(3\) for clockwise. For the clockwise case, \(-1+3=2\), which matches the original operation.

The common displacement across all operations is \(-\sum_{i=1}^{N}B_i\). Letting \(S\) be the sum of additional displacements, the condition for the final position is:

\(S-\sum_{i=1}^{N}B_i\equiv X\pmod M\)

which simplifies to:

\(S\equiv X+\sum_{i=1}^{N}B_i\pmod M\)

Therefore, it suffices to count the number of ways to choose additional displacements such that this equation holds.

Algorithm

DP State

Let dp[r] be the following count:

The number of sequences of operations processed so far such that the sum of additional displacements modulo \(M\) is \(r\).

Before any operations are performed, the sum of additional displacements is \(0\), so we initialize with:

  • dp[0] = 1
  • 0 for all others

Also, let offset store the sum of \(B_i\) modulo \(M\) up to the current operation, initialized to \(0\).

DP Transitions

For the \(i\)-th operation, let

\(s=(A_i+B_i)\bmod M\)

Since the additional displacement is either \(0\) or \(s\), there are two ways the remainder after the update can be \(r\):

  1. From a state with remainder \(r\), choose an additional displacement of \(0\).
  2. From a state with remainder \((r-s)\bmod M\), choose an additional displacement of \(s\).

Therefore, the transition formula is:

\(\mathrm{newdp}[r]=\bigl(\mathrm{dp}[r]+\mathrm{dp}[(r-s)\bmod M]\bigr)\bmod 998244353\)

Since these two cases correspond to different choices in the latest operation, they are counted separately even if they reach the same position. Moreover, every sequence of operations is uniquely classified into one of these two cases, so this transition counts them accurately and without omission.

In the code, an array rotated is created by cyclically shifting the array to the right by \(s\) positions:

rotated = dp[-shift:] + dp[:-shift]

This makes rotated[r] equal to dp[(r - shift) % m] before the update. Adding this element-wise to the original array performs the transition above:

dp = [(u + v) % MOD for u, v in zip(dp, rotated)]

At the same time, we update the offset for the baseline position:

offset = (offset + b) % m

Answer

After processing all operations, we need to count the sequences where the sum of additional displacements is \((X+\mathrm{offset})\bmod M\), so the answer is:

dp[(x + offset) % m]

Complexity

  • Time Complexity: \(O(NM)\)
    In each operation, we cyclically shift an array of length \(M\) and update each element.
  • Space Complexity: \(O(M)\)
    We only keep the current DP array and an array of length \(M\) used for updates.

Implementation Notes

  • It is sufficient to consider the rotation amount modulo \(M\).
    Since a full rotation does not change the position, the number of states does not increase even if \(A_i,B_i\) are large.

  • dp tracks the sum of additional displacements, not the actual position.
    The actual position is “sum of additional displacements \(-\mathrm{offset}\)”, so the index referenced at the end is (x + offset) % m.

  • Distinguish between choices even if they lead to the same position.
    Even when shift == 0, clockwise and counterclockwise are distinct choices. In this case, each dp[r] needs to be doubled.

  • The slicing works correctly even when shift == 0.
    In Python, dp[-0:] is the entire array and dp[:-0] is an empty array, so rotated will have the same contents as the original dp. As a result, each element is correctly doubled.

  • Update all states at once using the values before the update.
    Overwriting the array in-place risks computing as if the same operation were applied multiple times. In the code, a new array is created before assigning it back to dp.

    Source Code

import sys

MOD = 998244353


def main():
    input = sys.stdin.buffer.readline
    n, m, x = map(int, input().split())

    dp = [0] * m
    dp[0] = 1
    offset = 0

    for _ in range(n):
        a, b = map(int, input().split())
        shift = (a + b) % m
        offset = (offset + b) % m
        rotated = dp[-shift:] + dp[:-shift]
        dp = [(u + v) % MOD for u, v in zip(dp, rotated)]

    print(dp[(x + offset) % m])


if __name__ == "__main__":
    main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: