矩阵快速幂EOJ EOJ Monthly 2021.9 Sponsored by TuSimple A. Amazing Discovery
Posted biu~跃哥冲冲冲
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了矩阵快速幂EOJ EOJ Monthly 2021.9 Sponsored by TuSimple A. Amazing Discovery相关的知识,希望对你有一定的参考价值。
Problem A: Amazing Discovery
评测传送门
蒟蒻赛时就开了这么一个题,推了好久也还是推不出来😣
看了人家官方题解后,顺利AC。但是,这个推导过程有点搞怪啊,不是看了题解,我还真推不出来这个递推关系。
官方题解:
题解传送门
下面我提供一些推导过程中的式子,可自行代入验证一波。
这里提供一个可以进行数学计算的超强👉代数计算工具网站
S
1
=
2
a
S_1 =2a
S1=2a
S
2
=
2
a
2
+
2
b
S_2 = 2a^2+2b
S2=2a2+2b
S
3
=
2
a
3
+
6
a
b
S_3 = 2a^3+6ab
S3=2a3+6ab
S
4
=
2
a
4
+
2
b
2
+
12
a
2
b
S_4 = 2a^4+2b^2+12a^2b
S4=2a4+2b2+12a2b
S
5
=
2
a
5
+
10
a
b
2
+
20
a
3
b
S_5 = 2a^5+10ab^2+20a^3b
S5=2a5+10ab2+20a3b
S
5
=
2
a
∗
S
4
−
(
a
2
−
b
)
S
3
S_5 = 2a * S_4 - (a^2-b)S_3
S5=2a∗S4−(a2−b)S3
矩阵快速幂中A矩阵的构造:
[
S
n
,
S
n
+
1
]
[S_n,S_{n+1}]
[Sn,Sn+1] ×
A
A
A =
[
S
n
+
1
,
S
n
+
2
]
[S_{n+1},S_{n+2}]
[Sn+1,Sn+2]
A
=
[
0
b
−
a
2
1
2
a
]
A= \\begin{gathered} \\begin{bmatrix} 0 & b-a^2 \\\\ 1 & 2a \\end{bmatrix} \\end{gathered}
A=[01b−a22a]
初始矩阵为:
F
0
=
[
2
,
2
a
]
F0 = [2,2a]
F0=[2,2a]
之后直接矩阵快速幂计算即可。
AcCoding:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 2;
/*
2a * S[n-1] - S[n] = (a - b * b) * S[n - 2]
f0[2] = {S[n],S[n+1]}
A = {
{0,2a},
{1,b * b - a}
}
*/
void mul(ll c[][N], ll a[][N], ll b[][N]) {
static ll tmp[N][N];
memset(tmp, 0, sizeof tmp);
for (int i = 0;i < N;i++) {
for (int j = 0;j < N;j++) {
for (int k = 0;k < N;k++) {
(tmp[i][j] += a[i][k] % mod * b[k][j] % mod) %= mod;
}
}
}
memcpy(c, tmp, sizeof tmp);
}
int main() {
ll a, b, n; scanf("%lld%lld%lld", &a, &b, &n);
ll f[N][N] = { 2 * a, 2ll * a * a + 2ll* b };//n = 1
ll A[N][N] = {
{0,((b - a * a) % mod + mod) % mod},
{1,2ll * a % mod}
};
n--;
while (n) {
if (n & 1) mul(f, f, A);
mul(A, A, A);
n >>= 1;
}
ll res = (f[0][0] % mod + mod) % mod;
printf("%lld", res);
return 0;
}
以上是关于矩阵快速幂EOJ EOJ Monthly 2021.9 Sponsored by TuSimple A. Amazing Discovery的主要内容,如果未能解决你的问题,请参考以下文章
矩阵快速幂EOJ EOJ Monthly 2021.9 Sponsored by TuSimple A. Amazing Discovery
[EOJ Monthly 2018.10][C. 痛苦的 01 矩阵]
EOJ Monthly 2021.9 Sponsored by TuSimple(A)
EOJ Monthly 2020.7 Sponsored by TuSimpleC题(二维前缀和)
EOJ Monthly 2021.9 Sponsored by TuSimple——A.Amazing.Discovery(分治or二次剩余)