矩阵快速幂全WA求调

P3390 【模板】矩阵快速幂

```cpp # include <iostream> # define ll long long using namespace std; int n; const int mod = 1e9 + 7; struct Matrix{ ll m[101][101]; }; Matrix a; Matrix mul(Matrix x, Matrix y){ Matrix re; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++){ for (int k = 1; k <= n; k++) re.m[i][j] += x.m[i][k] * y.m[k][j]; re.m[i][j] %= mod; } return re; } Matrix ksm(int k, Matrix x){ Matrix ret; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i == j) ret.m[i][j] = 1; else ret.m[i][j] = 0; while (k){ if (k & 1) ret = mul(ret, x); k = k >> 1; x = mul(x, x); } return ret; } void print(Matrix m){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++) cout << m.m[i][j] << ' '; cout << endl; } } int main(){ int k; cin >> n >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a.m[i][j]; print(ksm(k, a)); return 0; } ``` 谢谢帮调,感激不尽。
by Ja50nY0un9_as_AgNO3 @ 2022-12-06 15:02:37


初步判断是全输出了 $0$。
by Ja50nY0un9_as_AgNO3 @ 2022-12-06 15:04:15


`x.m[i][k] * y.m[k][j]` 加个取模。 否则最大数可达 $(10^9+6)^2 \times n$。
by CuiZhenhang @ 2022-12-06 15:04:53


@[CuiZhenhang](/user/561644) 好的,谢谢,我去试试。
by Ja50nY0un9_as_AgNO3 @ 2022-12-06 15:05:37


@[CuiZhenhang](/user/561644) 貌似不行。
by Ja50nY0un9_as_AgNO3 @ 2022-12-06 15:06:21


`mul` 函数中的 `re` 没有初始清空。 建议 `memset`。
by CuiZhenhang @ 2022-12-06 15:06:45


@[CuiZhenhang](/user/561644) 没用,还是输出 $0$。
by Ja50nY0un9_as_AgNO3 @ 2022-12-06 15:08:16


注意 $0 \le k \le 10^{12}$
by CuiZhenhang @ 2022-12-06 15:09:15


@[CuiZhenhang](/user/561644) 但这不至于造成全 WA 吧。应该至少有一个点是 $k=0$。
by Ja50nY0un9_as_AgNO3 @ 2022-12-06 15:11:19


@[CuiZhenhang](/user/561644) 过了,谢谢。关注了。
by Ja50nY0un9_as_AgNO3 @ 2022-12-06 15:12:06


|