forked from rachitiitr/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix_exponentiation.cpp
More file actions
70 lines (61 loc) · 1.21 KB
/
Matrix_exponentiation.cpp
File metadata and controls
70 lines (61 loc) · 1.21 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
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
#define sp ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
int MAX = 3000;
struct matrix{
ll a[3000][3000];
int size;
matrix(int sz){
size=sz;
for (int i=0; i<size; ++i)
for (int k=0; k<size; ++k)
a[i][k]=0;
}
matrix operator * (matrix & b){
matrix c = matrix(b.size);
for (int i=0; i<size; ++i)
for (int k=0; k<size; ++k)
for (int j=0; j<size; ++j)
c.a[i][j]=(c.a[i][j]+ (1LL*a[i][k]*b.a[k][j])%mod)%mod;
return c;
}
};
matrix unit(int sz){
matrix z=matrix(sz);
for(int i=0; i<sz; ++i)
z.a[i][i]=1LL;
return z;
}
matrix modpow(matrix & m,ll n){
if ( n == 0 )
return unit(m.size);
matrix half =modpow(m,n>>1);
matrix out=half*half;
if(n&1)
out=out*m;
return out;
}
int main() {
sp;
int n;
cin>>n;
matrix m=matrix(n);
int b,c;
for(int i=1;i<n;i++)
{
cin>>b>>c;
b--,c--;
m.a[b][c]=1;
m.a[c][b]=1;
}
ll r,k;
cin>>r>>k;
r--;
matrix prod = modpow(m,k);
for(int i=0;i<n;i++)
cout<<prod.a[r][i]<<" ";
cout<<endl;
return 0;
}