bzoj5015 [Snoi2017]礼物 矩阵快速幂+二项式展开
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=5015
题解
设 \(f_i\) 表示第 \(i\) 个朋友的礼物,\(s_i\) 表示从 \(1\) 到 \(i\) 的 \(f_i\) 的和。
\[f_i = s_{i-1}+i^k\\s_i = s_{i-1}+f_i = 2s_{i-1}+i^k \]考虑用矩阵维护转移,但是这个 \(i^k\) 不太方便转移。
发现 \(k \leq 10\),可以考虑使用二项式展开。
\[(i+1)^k = \sum_{j=0}^k \binom{k}{i}i^j \]所以可以用矩阵维护一下 \(i^j(0 \leq j \leq k)\) 转移就可以了。
时间复杂度 \(O(k^3\log n)\)。
#include
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair pii;
template inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 13 + 3;
const int P = 1e9 + 7;
ll T;
int k, n;
inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
}
struct Matrix {
int a[N][N];
inline Matrix() { memset(a, 0, sizeof(a)); }
inline Matrix(const int &x) {
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; ++i) a[i][i] = x;
}
inline Matrix operator * (const Matrix &b) {
Matrix c;
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) sadd(c.a[i][j], (ll)a[i][k] * b.a[k][j] % P);
return c;
}
} A, B;
inline Matrix fpow(Matrix x, ll y) {
Matrix ans(1);
for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
return ans;
}
inline void work() {
B.a[3][1] = 1;
A.a[1][1] = 0, A.a[1][2] = 1;
A.a[2][1] = 0, A.a[2][2] = 2;
A.a[3][3] = 1;
for (int i = 4; i <= k + 3; ++i)
for (int j = 3; j <= i; ++j) A.a[i][j] = smod(A.a[i - 1][j - 1] + A.a[i - 1][j]);
for (int i = 1; i <= n; ++i) sadd(A.a[1][i], A.a[n][i]), sadd(A.a[2][i], A.a[n][i]);
printf("%d\n", B.a[1][1]);
}
inline void init() {
read(T), read(k);
n = k + 3;
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}