Official

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

claude5.5opus-high

Overview

In each of the \(N\) operations, we choose either “clockwise by \(A_i\)” or “counterclockwise by \(B_i\)”. We count the number of ways to make choices such that the final position is tick mark \(X\) using DP, where the state is the remainder modulo \(M\) (the position).

Analysis

Key Observation 1: There are only \(M\) possible positions The position of the pointer is determined by “the total rotation amount so far modulo \(M\).” Treating clockwise as \(+\) and counterclockwise as \(-\), the position updates as follows:

  • Choosing clockwise: \(j \to (j + A_i) \bmod M\)
  • Choosing counterclockwise: \(j \to (j - B_i) \bmod M\)

No matter how much it rotates, the position will always be one of the \(M\) values from \(0 \sim M-1\).

Key Observation 2: We only need to consider the rotation amounts modulo \(M\) \(A_i\) and \(B_i\) can be as large as \(10^9\). However, rotating by \(M\) tick marks completes a full circle and returns to the same position. Therefore, replacing them with \(a = A_i \bmod M\) and \(b = B_i \bmod M\) does not change the result.

Issues with a Naive Approach There are \(2^N\) possible ways to make the choices in total. For \(N = 2000\), a brute-force search will not finish in time (TLE). However, different sequences of choices that reach the same position midway will behave completely identically thereafter. Therefore, we can group them together and count “how many ways there are to reach position \(j\) after completing up to operation \(i\).”

Note: Two choices that lead to the same position must still be distinguished For example, consider \(M=4\) and \((A_i,B_i)=(1,3)\).

  • Rotating clockwise by \(1\) moves the position from \(0 \to 1\).
  • Rotating counterclockwise by \(3\) gives \(0 - 3 \equiv 1 \pmod 4\), which also results in position \(1\).

Since these are two distinct choices, we count \(2\) ways to reach position \(1\). The same applies when \(A_i = B_i = 0\); both transitions must be added separately.

Algorithm

Let \(dp[j]\) be defined as “the number of ways to make choices such that the pointer points to tick mark \(j\) after the operations processed so far.”

  1. The initial state is \(dp[0] = 1\), and all other values are \(0\).
  2. For each operation \(i\), compute \(a = A_i \bmod M\) and \(b = B_i \bmod M\). For all \(j\), perform the following two transitions to build a new array \(nd\):
    • \(nd[(j + a) \bmod M] \mathrel{+}= dp[j]\)
    • \(nd[(j - b) \bmod M] \mathrel{+}= dp[j]\)

After that, replace \(dp\) with \(nd\). 3. After all \(N\) operations are complete, \(dp[X]\) is the answer (keep all additions modulo \(998244353\)).

Concrete Example Consider the case where \(N=2\), \(M=3\), \(X=0\), and the operations are \((1,1)\) and \((2,0)\).

  • Initial: \(dp = [1,0,0]\)
  • 1st operation: From position \(0\), transitions are \(+1 \to 1\) and \(-1 \to 2\). Thus, \(dp = [0,1,1]\).
  • 2nd operation:
    • From position \(1\): \(+2 \to 0\), \(-0 \to 1\)
    • From position \(2\): \(+2 \to 1\), \(-0 \to 2\)
    • The result is \(dp = [1,2,1]\).

The answer is \(dp[0] = 1\).

Complexity

  • Time complexity: \(O(NM)\) (At most around \(2000 \times 2000 = 4 \times 10^6\), which is sufficiently fast)
  • Space complexity: \(O(M)\) (By reusing two arrays)

Implementation Notes

  • Handling large rotation amounts: Since \(A_i\) and \(B_i\) can be up to \(10^9\), read them as long long and take the remainder modulo \(M\) first.

  • Be careful with negative remainders: In C++, the modulo operation on a negative number yields a negative result. Therefore, make the counterclockwise position non-negative by adding \(M\), such as ((j - b) % m + m) % m.

  • Always add both transitions: Even if both choices transition to the same position, add them separately. This ensures that the two choices are properly distinguished.

  • Reusing arrays: Initialize the next array \(nd\) with \(0\) each time, and swap it with \(dp\) using swap after computing the transitions.

  • Modulo operation: Checking “if it is greater than or equal to \(998244353\), subtract \(998244353\)” after each addition reduces % operations and runs faster.

    Source Code

#include <bits/stdc++.h>
using namespace std;
const long long MOD=998244353;
int main(){
    int n,m,x;
    scanf("%d %d %d",&n,&m,&x);
    vector<long long> dp(m,0),nd(m);
    dp[0]=1;
    for(int i=0;i<n;i++){
        long long A,B;
        scanf("%lld %lld",&A,&B);
        int a=A%m,b=B%m;
        fill(nd.begin(),nd.end(),0);
        for(int j=0;j<m;j++){
            if(!dp[j])continue;
            int p=(j+a)%m;
            nd[p]+=dp[j]; if(nd[p]>=MOD)nd[p]-=MOD;
            int q=((j-b)%m+m)%m;
            nd[q]+=dp[j]; if(nd[q]>=MOD)nd[q]-=MOD;
        }
        swap(dp,nd);
    }
    printf("%lld\n",dp[x]%MOD);
}

This editorial was generated by claude5.5opus-high.

posted:
last update: