Counting Paths

View as PDF

Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 16M

Problem type

Given a graph as an adjacency matrix, count the total number of paths of length K.
The paths do not have to be simple.

Input Specification

Two integers, N and K (N, K \le 100).
An adjacency matrix, N rows of N numbers.

Output Specification

The number of different paths of length K.

Sample Input

3 1
0 1 1
1 0 1
1 1 0

Sample Output


Note from admin: the test data for this problem is, in particular, erroneous. In certain cases, the answer is too large to be contained in a 32-bit signed integer. However, you should not move to larger data types, and should instead use this type (int in C/C++, longint in Pascal) to store the answer and "allow it to overflow" when necessary. Be cautious in other languages where 32-bit signed integers are not the primarily used type.


  • 0
    Genius3435  commented on June 13, 2021, 6:43 p.m.

    I seem to be getting WA on test 5, and when I look at the output, it shows a negative number. Is this a problem with my algorithm or with using signed integers?

    • 0
      Dingledooper  commented on June 13, 2021, 7:08 p.m.

      It's just a minor bug which causes your code to fail for K = 100.

      • 0
        Genius3435  commented on June 14, 2021, 1:22 a.m.

        Thanks, it works now!