**Graph Paths — I**

I am trying to solve this problem using matrix exponentiation but it is giving Wrong Answer.

Can someone figure out what I am doing wrong.

# | User | Rating |
---|---|---|

1 | MiFaFaOvO | 3681 |

2 | Um_nik | 3553 |

3 | tourist | 3515 |

4 | Benq | 3508 |

5 | ecnerwala | 3390 |

6 | TLE | 3223 |

7 | scott_wu | 3209 |

8 | Petr | 3205 |

9 | ksun48 | 3197 |

10 | Radewoosh | 3188 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 204 |

2 | antontrygubO_o | 189 |

2 | pikmike | 189 |

4 | Monogon | 186 |

5 | vovuh | 184 |

6 | Ashishgup | 182 |

7 | Um_nik | 180 |

8 | SecondThread | 174 |

9 | Radewoosh | 172 |

10 | ko_osaga | 161 |

**Graph Paths — I**

I am trying to solve this problem using matrix exponentiation but it is giving Wrong Answer.

Can someone figure out what I am doing wrong.

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/15/2020 00:02:33 (f3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Auto comment: topic has been updated by DemonStar (previous revision, new revision, compare).There can be more than one path from a to b. So, change to: v[a-1][b-1]++;

Thanks, it worked.

Can you explain me how are you using matrix exponentiation here?

If 'M' is the adjacency matrix of an unweighted graph, then the matrix M raised to the power K gives the number of paths from a to b with exactly K edges for each node pair (a, b)

DemonStar I find it difficult to understand this explanation. I mean Why does it work?

Similar Problem: https://www.codechef.com/PCR12020/problems/STIMEMCH

Can you explain how do you use matrix expo here

This explains it in more detail https://cp-algorithms.com/graph/fixed_length_paths.html