Problems > Walks of Astronomical Length

Walks of Astronomical Length

Difficulty: 1800 6000 ms 128 MB

graphs matrix-exponentiation

Count the directed walks of EXACTLY K edges from node 1 to node N, modulo 109+7. K is up to 1018.

Input: the first line has N, M and K; each of the next M lines has a directed edge u v (multi-edges allowed and counted).

Output: the number of length-K walks mod 109+7.

Constraints:

  • 2 ≤ N ≤ 100
  • 1 ≤ M ≤ 104
  • 1 ≤ K ≤ 1018

Sample Tests

Input 1
3 3 2
1 2
2 3
1 3
Output 1
1
Input 2
2 1 1
1 2
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.