If this is the case during the competition, the first 50
At the beginning, everyone thought they were looking for a regular question, so they pushed the formula and found that it was similar to the Yang Hui triangle. So I went to seek the general item of the Yang Hui triangle.
So TLE went through countless times. (The maximum value of each fetch range must be more than 3 seconds ).
For an obvious matrix, it can be converted into a matrix operation, with each row being transferred. Is the power operation of a transfer matrix. Then use the quick matrix power.
A:
10 0 0 1
10 1 0 1
10 1 1 1
0 0 0 1
B:
23
0
0
3
C = a ^ m * B, ANS = C [N]
Lesson: for the time limit, even the maximum data can be delivered in seconds. Do not think it is just enough. The use of matrices is everywhere ···~~~~
# Pragma comment (linker, "/Stack: 1024000000,1024000000") # include <stdio. h >#include <string> typedef _ int64 ll; # define INF 10000007 # define Max 100000int n, m; ll a [12] [12]; // initll B [12]; // initll C [12] [12]; // ansll T [12] [12]; // templl d [12] [12]; void matrix (LL (* A) [12], LL (* B) [12], LL (* C) [12]) {int I, j, k; ll sum; for (I = 0; I <= n + 1; I ++) {for (j = 0; j <= n + 1; j ++) {sum = 0; For (k = 0; k <= n + 1; k ++) sum = sum + (A [I] [k] * B [k] [J]) % INF, Su M % = inf; C [I] [J] = sum ;}}// this error occurs at the beginning. Void CAL () {// memcpy (C, A, sizeof (a); int I, j, k; k = m; while (k) {I = K & 1; while (I = 0) {matrix (A, A, T); memcpy (A, T, sizeof (t )); k> = 1; I = K & 1;} matrix (C, A, T); memcpy (C, T, sizeof (t); matrix (a,, t); // After the quick computation, multiply it to the next bit. C is set to the unit matrix memcpy (A, T, sizeof (T) at the beginning )); k> = 1;}/* for (I = 0; I <= n + 1; I ++) {for (j = 0; j <= n + 1; j ++) printf ("% d", C [I] [J]); printf ("\ n") ;}*/for (I = 0; I <= n + 1; I ++) {ll sum = 0; For (j = 0; j <= n + 1; j ++) sum = sum + (C [I] [J] * B [J]) % INF, sum % = inf; A [0] [I] = sum;} printf ("% d \ n", a [0] [N]);} int main () {While (scanf ("% d", & N, & M )! = EOF) {int I, j; for (I = 1; I <= N; I ++) scanf ("% d", B + I ); B [I] = 3; B [0] = 23; memset (A, 0, sizeof (a); memset (C, 0, sizeof (c )); for (I = 0; I <= N; I ++) A [I] [0] = 10, a [I] [n + 1] = 1, c [I] [I] = 1; // unit matrix C [I] [I] = 1; A [n + 1] [n + 1] = 1; for (I = 1; I <n + 1; I ++) for (j = 1; j <= I; j ++) A [I] [J] = 1;/* for (I = 0; I <= n + 1; I ++) {for (j = 0; j <= n + 1; j ++) printf ("% d", a [I] [J]); printf ("\ n ");} */CAL ();} return 0 ;}
2014 ACM/ICPC Asia Regional Xi 'an online 233 matrix, HDU 5015