D - 回転舞台の操作記録 / Operation Log of the Rotating Stage 解説 by admin 印刷ページ
claude5.5opus-highOverview
This problem asks us to count the number of ways to choose either “\(+A_i\)” or “\(-B_i\)” for each of the \(N\) operations such that the total sum modulo \(M\) equals \(X\). This can be solved with dynamic programming (DP) using the position modulo \(M\) as the state.
Analysis
The position of the pointer is determined only by the value modulo \(M\)
When pointing to tick mark \(p\), rotating clockwise by \(A\) points to \((p+A) \bmod M\), and rotating counterclockwise by \(B\) points to \((p-B) \bmod M\). No matter how many full rotations are made, the result depends solely on the remainder. Therefore, it is sufficient to take \(A_i, B_i \pmod M\) at the beginning. Although \(A_i\) and \(B_i\) can be up to \(10^9\), doing this eliminates the need to handle huge values.A naive brute-force search is too slow
There are \(2^N\) possible ways to make the choices, which cannot possibly be enumerated for \(N=2000\). However, the history of previous choices is unnecessary; knowing only the current tick mark allows us to determine the next transitions. Since there are only \(M\) possible states, we can count the number of ways efficiently using DP.All choices are distinguished
Even if both choices lead to the same tick mark, such as when \(A_i \equiv B_i \pmod M\), they are counted as distinct ways. For example, if \(A_i = B_i = 0\), the position does not change, but the number of ways doubles. If we simply add the contributions from the clockwise and counterclockwise transitions in the DP, this distinction is naturally preserved.
Algorithm
Let \(dp[p]\) be the number of ways to choose operations so far such that the pointer points to tick mark \(p\).
- Initial state: \(dp[0] = 1\), and all other entries are \(0\).
- For the \(i\)-th operation, letting \(a = A_i \bmod M\) and \(b = B_i \bmod M\), the transition is as follows:
$\(dp'[q] = dp[(q-a) \bmod M] + dp[(q+b) \bmod M]\)$
- First term: The case of reaching \(q\) by rotating clockwise by \(a\), meaning the pointer was previously at \(q-a\).
- Second term: The case of reaching \(q\) by rotating counterclockwise by \(b\), meaning the pointer was previously at \(q+b\).
- After \(N\) updates, \(dp[X]\) is the answer.
Concrete example: Consider \(M=4\) and a single operation with \((A,B)=(1,1)\). Updating from \(dp=[1,0,0,0]\), clockwise rotation leads to tick mark \(1\), and counterclockwise rotation leads to tick mark \(3\). The result is \(dp=[0,1,0,1]\).
Complexity
- Time complexity: \(O(NM)\) (at most about \(4 \times 10^6\) operations)
- Space complexity: \(O(M)\)
Implementation Notes
Write cyclic shifts using slices
Since the transition is the sum of two cyclic shifts of the array, it can be written concisely and quickly using Python list slicing.dp[m-a:] + dp[:m-a]is the array where each index \(q\) contains \(dp[q-a]\) (contribution from the clockwise rotation).dp[b:] + dp[:b]is the array where each index \(q\) contains \(dp[q+b]\) (contribution from the counterclockwise rotation).- Even when \(a=0\),
dp[m:] + dp[:m]results in the original array itself, so no special case handling is needed.
Be careful with the shift direction
Clockwise means the original position was \(q-a\), and counterclockwise means the original position was \(q+b\). Mixing up the signs will lead to an incorrect answer.Take the modulo at each step
Each time the two arrays are added usingzip, take the remainder modulo \(998244353\) to prevent the numbers from becoming too large.Source Code
import sys
def main():
data=sys.stdin.buffer.read().split()
n=int(data[0]); m=int(data[1]); x=int(data[2])
MOD=998244353
dp=[0]*m
dp[0]=1
idx=3
for _ in range(n):
a=int(data[idx])%m; b=int(data[idx+1])%m; idx+=2
r1=dp[m-a:]+dp[:m-a]
r2=dp[b:]+dp[:b]
dp=[(u+v)%MOD for u,v in zip(r1,r2)]
print(dp[x]%MOD)
main()
This editorial was generated by claude5.5opus-high.
投稿日時:
最終更新: