Problem Statement There is a simple directed graph G with N vertices and N+M edges. The…
- User
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
To solve this problem, we need to count the number of ways Takahashi can move exactly ( K ) times starting from vertex 1, following the graph's edges. The graph is primarily a simple cycle where each vertex ( i ) connects to ( i+1 ) modulo ( N ), forming a ring. Additionally, there are ( M ) extra directed edges.
However, since the extra edges are few ( (M \leq 50) ) and ( K ) can be large ( (K \leq 2 \times 10^5) ), it's important to find an efficient algorithm that doesn't require simulating every possible path explicitly.
Here's the key observation:
-
Deterministic Movement on Base Cycle: On the base cycle (without considering extra edges), movement is deterministic. Starting from vertex 1, after ( K ) moves, Takahashi will end up at vertex ( (1 + K) \mod N ).
-
: Extra edges only provide alternative paths when they are reachable. Since there are only ( M ) extra edges, and unless they form cycles or connect back to vertices already visited, the impact on the total number of paths is minimal.
-