Problem Statement
There is a simple directed graph
G with
N vertices and
N+M edges. The vertices are numbered
1 to
N, and the edges are numbered
1 to
N+M.
Edge
i goes from vertex
i to vertex
i+1. (Here, vertex
N+1 is considered as vertex
1.)
Edge
N+i
(1≤i≤M) goes from vertex
X
i
to vertex
Y
i
.
Takahashi is at vertex
At each vertex, he can move to any vertex to which there is an outgoing edge from the current vertex.
Compute the number of ways he can move exactly
K times.
That is, find the number of integer sequences
(v
0
,v
1
,…,v
K
) of length
K+1 satisfying all of the following three conditions:
1≤v
i
≤N for
i=0,1,…,K.
v
0
=1.
There is a directed edge from vertex
v
i−1
to vertex
v
i
for
i=1,2,…,K.
Since this number can be very large, print it modulo
998244353.
Constraints
2≤N≤2×10
5
0≤M≤50
1≤K≤2×10
5
1≤X
i
,Y
i
≤N,
X
i
=Y
i
All of the
N+M directed edges are distinct.
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
M
K
X
1
Y
1
X
2
Y
2
⋮
X
M
Y
M
Output
Print the count modulo
998244353.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To solve this problem efficiently, we can leverage the properties of circulant matrices and use the Number Theoretic Transform (NTT), which is a special case of Fourier Transform suitable for modulus operations commonly used in competitive programming. The modulus 998244353 is appropriate for the NTT.
Here's the step-by-step approach:
Modeling the Problem:
Think of the problem in terms of polynomial representations where positions correspond to exponents in polynomials.
The base movement along the cycle corresponds to a shift, which can be represented by ( x^1 ).
Each extra edge adds an additional term to the polynomial representing possible moves from a vertex.
Construct the Transition Polynomial ( S(x) ):
Initialize ( S(x) ) as ( x^1 ) for the cycle edge.
For each extra edge from ( X_i ) to ( Y_i ), add ( x^{(Y_i - X_i) \mod N} ) to ( S(x) ).
The constructed polynomial ( S(x) = x^1 + \sum_{i=1}^{M} x^{(Y_i - X_i) \mod N} ) represents all possible transitions at each step.
Compute ( S(x)^K ) Using NTT:
Pad ( S(x) ) to the next power of two for the NTT.
Perform the forward NTT on ( S(x) ) to obtain ( S_{\text{hat}}[k] ).
Raise each component ( S_{\text{hat}}[k] ) to the ( K^{th} ) power modulo 998244353.
Perform the inverse NTT to get the coefficients of ( S(x)^K ).
Calculate the Final Answer:
Sum all coefficients in the resulting polynomial ( S(x)^K ) corresponding to positions ( 0 ) to ( N-1 ).
This sum represents the total number of ways Takahashi can move exactly ( K ) times.
Implementation Details:
Use optimized NTT routines that handle the modulus 998244353.
Ensure careful padding and indexing to maintain correctness.
Handle any potential special cases, like zero coefficients.
Below is the implementation showcasing this approach:
python
MOD = 998244353
ROOT = 3 # Primitive root modulo MOD
# Precomputed inverses, roots, etc., required for NTT
import sys
import threading
def main():
import math
N, M, K = map(int, sys.stdin.readline().split())
N2 = 1
while N2 < 2*N:
N2 *= 2
S = [0] * N2
# Cycle edge
S[1] += 1
# Read and process extra edges
for _ in range(M):
X_i, Y_i = map(int, sys.stdin.readline().split())
shift_d = (Y_i - X_i) % N
S[shift_d] += 1
# Perform NTT
from numpy.fft import fft, ifft
# Since we're dealing with modulus, we implement NTT instead of FFT
# Here, we'd typically code up the NTT, but for brevity and clarity,
# we can use the built-in FFT and adjust for modulus
# However, we need to implement our own NTT to handle the modulus correctly
def ntt(a, invert):
n = len(a)
j = 0
for i in range(1, n):
bit = n >> 1
while j & bit:
j ^= bit
bit >>= 1
j ^= bit
if i < j:
a[i], a[j] = a[j], a[i]
len_ = 2
while len_ <= n:
wlen = pow(ROOT, (MOD -1)//len_, MOD)
if invert:
wlen = pow(wlen, MOD - 2, MOD)
for i in range(0, n, len_):
w = 1
for j in range(i, i+len_//2):
u = a[j]
v = a[j + len_//2] * w % MOD
a[j] = (u + v) % MOD
a[j + len_//2] = (u - v + MOD) % MOD
w = w * wlen % MOD
len_ <<=1
if invert:
inv_n = pow(n, MOD-2, MOD)
for i in range(n):
a[i] = a[i] * inv_n % MOD
# Pad S to size N2
a = S + [0]*(N2 - len(S))
ntt(a, False)
# Raise to Kth power
for i in range(N2):
a[i] = pow(a[i], K, MOD)
ntt(a, True)
ans = sum(a[:N]) % MOD
print(ans)
threading.Thread(target=main).start()
This code reads the input, constructs the transition polynomial, computes the NTT, raises to the ( K^{th} ) power, performs the inverse NTT, and finally sums up the coefficients to get the answer, all under modulo 998244353. This approach is efficient and suitable for the given constraints.