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
(1≤i≤N) 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.
Assistant
To solve this problem, we'll model the number of ways Takahashi can move exactly ( K ) times in the given directed graph using matrix exponentiation.
Understanding the Graph Structure:
We have a directed cycle formed by the first ( N ) edges, connecting vertex ( i ) to ( i+1 ) (with vertex ( N+1 ) considered as vertex 1).
There are additional ( M ) edges, each connecting vertex ( X_i ) to ( Y_i ).
Approach:
Represent the Graph as an Adjacency Matrix:
Since each vertex can have multiple outgoing edges (due to the additional ( M ) edges), we'll represent the graph using an adjacency matrix ( A ) of size ( N \times N ).
( A[i][j] = 1 ) if there's an edge from vertex ( i ) to vertex ( j ); otherwise, ( A[i][j] = 0 ).
Initial State Vector:
We define a state vector ( v_0 ) of size ( N ), where ( v_0[1] = 1 ) (since Takahashi starts at vertex 1) and all other entries are 0.
Compute ( v_K = v_0 \times A^K ):
( A^K ) represents the number of ways to reach each vertex after ( K ) steps.
We can compute ( A^K ) using fast matrix exponentiation.
Calculating the Total Number of Ways:
The total number of ways Takahashi can move exactly ( K ) times is the sum of all entries in ( v_K ).
This is because each entry ( v_K[i] ) represents the number of ways to reach vertex ( i ) after ( K ) steps.
Optimizations:
Sparse Matrix Multiplication:
Since the adjacency matrix ( A ) is sparse (most of its entries are 0), we can optimize matrix multiplication by only considering the non-zero entries.
Modulo Operation:
Perform all calculations under modulo ( 998244353 ) to prevent integer overflows and adhere to the problem's requirement.
Implementation Steps:
Build the Adjacency List:
Create an adjacency list or dictionary to represent the graph efficiently.
Exponentiate the Matrix:
Implement a function to perform matrix exponentiation using recursion and memoization.
Multiplication Function:
Implement a function to multiply two matrices or a matrix and a vector, considering only the non-zero entries.
Compute the Result:
Use the exponentiation and multiplication functions to compute ( v_K ).
Sum all the entries of ( v_K ) to get the total number of ways.
Example Code:
python
MOD = 998244353
def multiply_vector_matrix(v, A):
result = [0] * len(v)
for i in range(len(v)):
if v[i]:
for j in A[i]:
result[j] = (result[j] + v[i]) % MOD
return result
def power_matrix(A, K):
if K == 1:
return A
elif K % 2 == 1:
return multiply_matrices(power_matrix(A, K - 1), A)
else:
half = power_matrix(A, K // 2)
return multiply_matrices(half, half)
def multiply_matrices(A1, A2):
n = len(A1)
result = [{} for _ in range(n)]
for i in range(n):
for k in A1[i]:
for j in A2[k]:
if j in result[i]:
result[i][j] = (result[i][j] + A1[i][k] * A2[k][j]) % MOD
else:
result[i][j] = (A1[i][k] * A2[k][j]) % MOD
return result
def solve(N, M, K, X, Y):
# Build adjacency list
A = [[] for _ in range(N)]
for i in range(N):
A[i].append((i + 1) % N)
for xi, yi in zip(X, Y):
A[xi - 1].append(yi - 1)
# Convert to adjacency matrix with counts
matrix = [{} for _ in range(N)]
for i in range(N):
for j in A[i]:
if j in matrix[i]:
matrix[i][j] += 1
else:
matrix[i][j] = 1
# Compute v_K
v = [0] * N
v[0] = 1
K_binary = bin(K)[2:]
matrices = {}
current_power = matrix
for i in reversed(range(len(K_binary))):
if K_binary[i] == '1':
if i in matrices:
v = multiply_vector_matrix(v, matrices[i])
else:
matrices[i] = current_power
v = multiply_vector_matrix(v, current_power)
current_power = multiply_matrices(current_power, current_power)
# Sum the result
return sum(v) % MOD
# Example usage:
# N, M, K = map(int, input().split())
# X = []
# Y = []
# for _ in range(M):
# xi, yi = map(int, input().split())
# X.append(xi)
# Y.append(yi)
# print(solve(N, M, K, X, Y))
Note:
Since the size of ( K ) could be large, we use binary exponentiation to compute ( A^K ) efficiently.
The code assumes that the input is provided correctly, and the function solve will return the correct number of ways modulo ( 998244353 ).
In summary, we use matrix exponentiation optimized for sparse matrices to compute the total number of ways Takahashi can move exactly ( K ) times in the given directed graph.