CF1105C Ayoub and Lost Array
\(\large{题目链接}\)
\(\\\)
\(\Large\textbf{Solution: } \large{这道题我一开始以为是推式子直接算,然后想了好久都没想出来。查看标签,发现是dp。\\首先预处理出区间中膜3余1、2、0的数有多少个,因为我们只关心余数。设f_{i,j}表示到第i个数字余数为j的方案个数,那么直接进行转移即可。时间复杂度\text{O(n)}。\\查看题解,发现可以矩阵加速递推,我还是太菜了,没有想到。\\设a是膜3余数为0的个数,b是余1,c是余2,那么可以写成这么一个矩乘,直接快速幂即可。时间复杂度\text{O(logn)}。\\\begin{bmatrix}
f_{0} & f_{1} & f_{2}
\end{bmatrix} \times \begin{bmatrix}
a & b & c \\
c & a & b \\
b & c & a
\end{bmatrix}}\)
\(\\\)
\(\Large\textbf{Summary: } \large{1.并不是所有数学题都能推式子,有些也与dp相关。\\2.对于递推,如果能找到多个变量的一个固定关系,可以考虑矩阵加速。}\)
\(\\\)
\(\Large\textbf{Code: }\)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int p = 1e9 + 7;
const int N = 2e5 + 5;
ll n, f[N][3];
int main() {
ios::sync_with_stdio(false);
ll l, r;
cin >> n >> l >> r;
ll a, b, c;
a = r / 3 - (l - 1) / 3;
b = (r + 2) / 3 - (l + 1) / 3;
c = (r + 1) / 3 - l / 3;
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
f[i][0] = ((f[i - 1][0] * a) % p + (f[i - 1][1] * c) % p + (f[i - 1][2] * b) % p) % p;
f[i][1] = ((f[i - 1][0] * b) % p + (f[i - 1][1] * a) % p + (f[i - 1][2] * c) % p) % p;
f[i][2] = ((f[i - 1][0] * c) % p + (f[i - 1][1] * b) % p + (f[i - 1][2] * a) % p) % p;
}
cout << f[n][0] << endl;
return 0;
}
\(\Large\textbf{Code: }\)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
const int p = 1e9 + 7;
int n;
struct juz {
int a[5][5];
juz () {
memset(a, 0, sizeof (a));
}
};
juz operator * (const juz x, const juz y) {
juz z;
for (int k = 1; k <= 3; ++k)
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
z.a[i][j] = (1ll * z.a[i][j] + 1ll * x.a[i][k] * y.a[k][j] % p) % p;
return z;
}
int main() {
ios::sync_with_stdio(false);
int l, r;
cin >> n >> l >> r;
int a, b, c;
a = r / 3 - (l - 1) / 3;
b = (r + 2) / 3 - (l + 1) / 3;
c = (r + 1) / 3 - l / 3;
juz m, ans;
ans.a[1][1] = 1;
m.a[1][1] = m.a[2][2] = m.a[3][3] = a;
m.a[1][2] = m.a[2][3] = m.a[3][1] = b;
m.a[1][3] = m.a[2][1] = m.a[3][2] = c;
for (; n ; n >>= 1, m = m * m) if (n & 1) ans = ans * m;
cout << ans.a[1][1];
return 0;
}