-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfRoutings.cpp
More file actions
59 lines (47 loc) · 1.18 KB
/
NumberOfRoutings.cpp
File metadata and controls
59 lines (47 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Solution to this problem: https://www.iarcs.org.in/inoi/contests/oct2005/Advanced-2.php
#include <iostream>
#include <vector>
#include <String.h>
using namespace std;
vector< vector< int > > mult (vector< vector< int > > mat1, vector< vector< int > > mat2)
{
int size = mat1.size();
vector< vector< int > > final( size, vector< int > ( size, 0 ) );
for( int i = 0; i < size; i++ )
{
for( int j = 0; j < size; j++ )
{
for( int k = 0; k < size; k++ )
{
final[i][j] = ( final[i][j] + mat1[i][k] * mat2[k][j] ) % 42373;
}
}
}
return final;
}
int main ()
{
int n;
cin >> n;
vector<vector<int>> matr (n, vector<int> (n, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> matr[i][j];
}
}
int src, dest, k;
cin >> src >> dest >> k;
vector<vector<int>> final (n, vector<int> (n, 0));
for (int i = 0; i < n; ++i) {
final[i][i] = 1;
}
int currK = k;
while (currK > 0) {
if (currK % 2) {
final = mult(final, matr);
}
matr = mult(matr, matr);
currK = currK / 2;
};
cout << final[src-1][dest-1] % 42373 << endl;
}