Problem descriptionin our daily life we often use 233 to express our feelings. actually, we may say 2333,233 33, or 233333... in the same meaning. and here is the question: Suppose we have a matrix called 233 matrix. in the first line, it wocould be 233,233 3, 23333... (It means A0, 1 = 233, A0, 2 = 2333, A0, 3 = 23333 ...) besides, in 233 matrix, we got AI, j = ai-1, J + AI, J-1 (I, j = 0 ). now you have known A1, 0, A2, 0,..., An, 0, cocould you tell me an, m in the 233 matrix?
Inputthere are multiple test cases. Please process till EOF.
For each case, the first line contains two postive integers n, m (n ≤ 10, M ≤109 ). the second line contains N integers, A1, 0, A2, 0 ,..., an, 0 (0 ≤ AI, 0 <231 ).
Outputfor each case, output an, M mod 10000007.
Sample Input
1 112 20 03 723 47 16
Sample output
234279972937Hint
Source2014 ACM/ICPC Asia Regional Xi 'an online
Question: calculate the value of the last digit.
Train of Thought: Move the matrices after line I back to the I bit to construct a matrix:
10 0 0 0 0... 1
1 1 0 0 0... 0
0 1 1 0... 0
................
0 0 0 0 0... 1,
However, an initial matrix with the B dimension n + 2*1 needs to be released, which is an initial Column Based on the movement.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef __int64 ll;using namespace std;const int maxn = 20;const int mod = 10000007;struct Matrix{int n;ll v[maxn][maxn];Matrix(int _n = maxn) {n = _n;}void init(ll _v = 0) {memset(v, 0, sizeof(v));if (_v)for (int i = 0; i < n; i++)v[i][i] = _v;}void output() {for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++)printf("%I64d ", v[i][j]);puts("");}puts("");}} a, b, c;Matrix operator * (Matrix a, Matrix b) {Matrix c(a.n);for (int i = 0; i < a.n; i++) {for (int j = 0; j < a.n; j++) {c.v[i][j] = 0;for (int k = 0; k < a.n; k++) {c.v[i][j] += (a.v[i][k] * b.v[k][j]) % mod;c.v[i][j] %= mod;}}}return c;}Matrix operator ^ (Matrix a, ll k) {Matrix c(a.n);c.init(1);while (k) {if (k & 1)c = a * c;a = a * a;k >>= 1;}return c;}Matrix operator + (Matrix a, Matrix b) {Matrix c(a.n);for (int i = 0; i < a.n; i++)for (int j = 0; j < a.n; j++)c.v[i][j] = (b.v[i][j] + a.v[i][j]) % mod;return c;}Matrix operator + (Matrix a, ll b) {Matrix c = a;for (int i = 0; i < a.n; i++)c.v[i][i] = (a.v[i][i] + b) % mod;return c;}ll n, m, con[maxn];ll f[maxn], f2[maxn];int main() {while (scanf("%I64d%I64d", &n, &m) != EOF) {for (int i = 1; i <= n; i++)scanf("%I64d", &con[i]);memset(f, 0, sizeof(f));f[0] = 233;f[1] = con[1];for (int i = 2; i <= n; i++) {memcpy(f2, f, sizeof(f));for (int j = 1; j < 10; j++)f[j] = f2[j] + f2[j-1];f[i] = con[i];f[0] = f[0] * 10 + 3;}a.init();a.n = n + 2;a.v[0][0] = 10;a.v[0][n+1] = 1;for (int i = 1; i <= n; i++)a.v[i][i-1] = a.v[i][i] = 1;a.v[n+1][n+1] = 1;b.init();b.n = n+2;for (int i = 0; i <= n; i++)b.v[i][0] = f[i];b.v[n+1][0] = 3;c = a ^ m;c = c * b;printf("%I64d\n", c.v[n][0]);}return 0;}
HDU-5015 233 matrix (matrix Construction)