-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbzoj3583.cpp
120 lines (120 loc) · 3.05 KB
/
bzoj3583.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <climits>
#include <cstdio>
#include <cstring>
int read() {
int res = 0, c;
do
c = getchar();
while(c < '0' || c > '9');
while('0' <= c && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
return res;
}
const int maxn = 1005, maxk = 25, mod = 1000000007;
typedef long long Int64;
#define asInt64 static_cast<Int64>
const Int64 end =
LLONG_MAX - asInt64(mod - 1) * (mod - 1);
struct Mat {
Int64 A[maxk][maxk];
int n;
Mat(int n) : n(n) {
memset(A, 0, sizeof(A));
}
const Int64* operator[](int id) const {
return A[id];
}
Int64* operator[](int id) {
return A[id];
}
Mat operator*(const Mat& rhs) const {
Mat res(n);
for(int i = 0; i <= n; ++i)
for(int k = 0; k <= n; ++k) {
Int64 mul = A[i][k];
if(mul == 0)
continue;
for(int j = 0; j <= n; ++j) {
res[i][j] += mul * rhs[k][j];
if(res[i][j] > end)
res[i][j] %= mod;
}
}
for(int i = 0; i <= n; ++i)
for(int j = 0; j <= n; ++j)
res[i][j] %= mod;
return res;
}
};
Mat powm(Mat A, int k) {
Mat res(A.n);
for(int i = 0; i <= A.n; ++i)
res[i][i] = 1;
while(k) {
if(k & 1)
res = res * A;
k >>= 1, A = A * A;
}
return res;
}
int out[maxn][maxk], in[maxk][maxn];
Mat buildMat(int N, int K) {
Mat res(K);
for(int i = 0; i <= K; ++i)
for(int k = 0; k <= N; ++k) {
Int64 mul = in[i][k];
if(mul == 0)
continue;
for(int j = 0; j <= K; ++j) {
res[i][j] += mul * out[k][j];
if(res[i][j] > end)
res[i][j] %= mod;
}
}
for(int i = 0; i <= K; ++i)
for(int j = 0; j <= K; ++j)
res[i][j] %= mod;
return res;
}
int main() {
int n = read();
int k = read();
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= k; ++j)
out[i][j] = read() % mod;
for(int j = 1; j <= k; ++j)
in[j][i] = read() % mod;
}
int m = read();
in[0][0] = out[0][0] = 1;
for(int t = 0; t < m; ++t) {
int u = read();
int v = read();
int d = read();
if(d == 0)
puts(u == v ? "1" : "0");
else if(d == 1) {
Int64 res = u == v;
for(int i = 1; i <= k; ++i)
res = (res +
asInt64(out[u][i]) * in[i][v]) %
mod;
printf("%lld\n", res);
} else {
out[v][0] = 1;
Mat base = buildMat(n, k);
Mat pwb = powm(base, d);
Int64 res = 0;
for(int i = 0; i <= k; ++i)
res =
(res +
asInt64(out[u][i]) * pwb[i][0]) %
mod;
printf("%lld\n", res);
out[v][0] = 0;
}
}
return 0;
}