HDU 5015 233 Matrix
矩阵快速幂。
首先得到这个式子:
依据这个,就可以构造矩阵。
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; long long const MOD = 10000007; int n, m; long long a[20]; struct Matrix { long long A[20][20]; int R, C; Matrix operator*(Matrix b); }; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b) { Matrix c; memset(c.A, 0, sizeof(c.A)); int i, j, k; for (i = 1; i <= R; i++) for (j = 1; j <= b.C; j++) for (k = 1; k <= C; k++) c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % MOD) % MOD; c.R = R; c.C = b.C; return c; } void init() { memset(X.A, 0, sizeof X.A); memset(Y.A, 0, sizeof Y.A); memset(Z.A, 0, sizeof Z.A); Z.R = n + 2; Z.C = 1; for (int i = 1; i <= n; i++) Z.A[i][1] = a[i]; Z.A[n + 1][1] = 23; Z.A[n + 2][1] = 3; X.R = n + 2; X.C = n + 2; for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) X.A[i][j] = 1; for (int i = 1; i <= n + 1; i++) X.A[i][n + 1] = 10; for (int i = 1; i <= n + 2; i++) X.A[i][n + 2] = 1; Y.R = n + 2; Y.C = n + 2; for (int i = 1; i <= n + 2; i++) Y.A[i][i] = 1; } void read() { for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); a[i] = a[i] % MOD; } } void work() { while (m) { if (m % 2 == 1) Y = Y*X; m = m >> 1; X = X*X; } Z = Y*Z; printf("%lld\n", Z.A[n][1]); } int main() { while (~scanf("%d%d", &n, &m)) { read(); init(); work(); } return 0; }