HDU 5015 233 Matrix (Xi'an network competition I), hdu5015
HDU 5015 233 Matrix
Question Link
Train of Thought: the rapid power of the matrix. No column is observed. The first column is added to the top of the left side, and the second column can be split to the top two and the top two on the left side, the third one can be split into three columns on the left plus the top. In this way, you only need to construct a matrix of 233 of each column and the column on the right of each column, and then perform a matrix's quick power.
Code:
#include <cstdio>#include <cstring>typedef long long ll;const int N = 15;const int MOD = 10000007;int n, m;struct mat {ll v[N][N];mat() {memset(v, 0, sizeof(v));}mat operator * (mat c) {mat ans;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)for (int k = 0; k < n; k++)ans.v[i][j] = (ans.v[i][j] + v[i][k] * c.v[k][j] % MOD) % MOD;return ans;}};mat pow_mod(mat x, int k) {mat ans;for (int i = 0; i < n; i++)ans.v[i][i] = 1;while (k) {if (k&1) ans = ans * x;x = x * x;k >>= 1;}return ans;}int main() {while (~scanf("%d%d", &n, &m)) {mat A;A.v[0][0] = 1;A.v[1][0] = 1; A.v[1][1] = 10;n += 2;for (int i = 2; i < n; i++) {for (int j = 1; j <= i; j++)A.v[i][j] = 1;}A = pow_mod(A, m);ll ans = 0;ll tmp[N];tmp[0] = 3; tmp[1] = 233;for (int i = 2; i < n; i++)scanf("%I64d", &tmp[i]);for (int i = 0; i < n; i++)ans = (ans + tmp[i] * A.v[n - 1][i] % MOD) % MOD;printf("%I64d\n", ans);}return 0;}