D - 回転舞台の操作記録 / Operation Log of the Rotating Stage Editorial by admin
Gemini 3.8 Flash (High)Overview
On a circle with \(M\) equally spaced tick marks (numbered \(0\) to \(M-1\)), we perform \(N\) operations where in each step we choose to move either \(A_i\) steps clockwise or \(B_i\) steps counterclockwise. The problem asks us to find the number of sequences of operations that end at mark \(X\), modulo \(998244353\).
Analysis
1. Periodicity and Rephrasing with Modulo (\(\bmod M\))
Since one full lap consists of \(M\) tick marks, positions on the circle can all be considered in the world of “remainders modulo \(M\)” (\(\bmod M\)). Although \(A_i\) and \(B_i\) can be as large as \(10^9\), completing any number of full laps brings us back to the same tick mark, so we can consider their remainders modulo \(M\) in advance without issue.
- Move clockwise by \(A_i\): The current position changes by \(+ (A_i \bmod M)\).
- Move counterclockwise by \(B_i\): The current position changes by \(- (B_i \bmod M)\). Converting this to clockwise movement is equivalent to moving \(+ (M - (B_i \bmod M)) \bmod M\) clockwise.
In this way, both operations can be unified into “moving clockwise by a certain amount.”
2. Limitations of Brute Force
For each operation, there are \(2\) choices: “clockwise” or “counterclockwise”. Therefore, a brute-force search that explores all sequences of choices would have \(2^N\) possibilities. In this problem, \(N \le 2000\), so \(2^{2000}\) is an astronomical number, making it impossible to pass within the time limit (resulting in TLE).
3. Speeding Up with Dynamic Programming (DP)
Regardless of the specific path taken during intermediate operations, as long as we know which tick mark we are on after the \(i\)-th operation, we can compute the subsequent transitions. Since the total number of tick marks \(M\) is at most \(2000\), we can aggregate states by “current tick mark.”
Therefore, by using dynamic programming (DP) to aggregate the number of ways to reach the same tick mark, we can solve this problem efficiently.
Algorithm
We use the following dynamic programming (DP) approach.
DP Table Definition
- \(dp[j]\): The number of sequences of choices that end at tick mark \(j\) after the current operation (\(\bmod 998244353\)).
Initial State
Initially, we are at tick mark \(0\), so we initialize as follows: - \(dp[0] = 1\) - \(dp[j] = 0 \quad (j \ne 0)\)
Transitions
Consider the operations \(i = 1, 2, \ldots, N\) in order. For each operation, let the clockwise movement amount be \(a = A_i \bmod M\), and the counterclockwise movement amount converted to clockwise be \(b = (M - (B_i \bmod M)) \bmod M\).
Based on the values from the previous step, initialize a new array \(next\_dp\) filled with \(0\), and for each tick mark \(j\) (\(0 \le j < M\)), add as follows: - \(next\_dp[(j + a) \bmod M] \leftarrow next\_dp[(j + a) \bmod M] + dp[j]\) - \(next\_dp[(j + b) \bmod M] \leftarrow next\_dp[(j + b) \bmod M] + dp[j]\)
After computing this for all \(j\), update \(dp\) with \(next\_dp\).
Answer
The value of \(dp[X]\) after completing all \(N\) operations is the desired answer.
Complexity
- Time Complexity: \(O(NM)\) For each of the \(N\) operations, we compute transitions from \(M\) tick marks (with 2 transitions each). Since \(N, M \le 2000\), this requires around \(N \times M \le 4 \times 10^6\) operations, which easily runs well within the time limit (typically 2 seconds).
- Space Complexity: \(O(M)\) It suffices to keep only the previous state and the next state, so we only need two arrays of size \(M\), using very little memory.
Implementation Notes
Modulo arithmetic for counterclockwise moves: In many programming languages, the modulo operator
%returns a negative value for negative numbers. By computing the counterclockwise movement as(M - (B_i % M)) % M, the result is guaranteed to fall in the range from \(0\) to \(M-1\) (taking% Magain on the outside ensures that the result is not \(M\) when \(B_i \% M = 0\)).Speeding up transitions: Since \(j + a\) and \(j + b\) are at most \(2M - 2\), instead of using the modulo operator
% M, you can use subtraction likeif (ja >= M) ja -= M;to further reduce the execution time.Source Code
#include <iostream>
#include <vector>
#include <atcoder/modint>
using namespace std;
using mint = atcoder::modint998244353;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, x;
if (!(cin >> n >> m >> x)) return 0;
vector<mint> dp(m, 0);
dp[0] = 1;
for (int i = 0; i < n; ++i) {
long long a_in, b_in;
cin >> a_in >> b_in;
int a = a_in % m;
int b = (m - (b_in % m)) % m;
vector<mint> next_dp(m, 0);
for (int j = 0; j < m; ++j) {
if (dp[j].val() == 0) continue;
int ja = j + a;
if (ja >= m) ja -= m;
next_dp[ja] += dp[j];
int jb = j + b;
if (jb >= m) jb -= m;
next_dp[jb] += dp[j];
}
dp = move(next_dp);
}
cout << dp[x].val() << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.