1. 程式人生 > >233 Matrix hdu 5015 矩陣快速冪

233 Matrix hdu 5015 矩陣快速冪

Problem Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 … in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 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, could you tell me an,m in the 233 matrix?

Input There 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).

Output For each case, output an,m mod 10000007.

Sample Input

1 1 1 2 2 0 0 3 7 23 47 16

Sample Output

234 2799 72937

Hint

Source 2014 ACM/ICPC Asia Regional Xi’an Online

推導

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include
<queue>
#include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> #include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 200005 #define inf 0x3f3f3f3f #define INF 0x7fffffff #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const int mod = 10000007; #define Mod 20100403 #define sq(x) (x)*(x) #define eps 1e-10 const int N = 1505; inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } struct matrix { ll Mat[20][20]; matrix() { ms(Mat); } }; int n, m; matrix multi(matrix A, matrix B) { matrix C; for (int i = 1; i <= n + 2; i++) { for (int j = 1; j <= n + 2; j++) { for (int k = 1; k <= n + 2; k++) { C.Mat[i][j] = (C.Mat[i][j] + A.Mat[i][k] * B.Mat[k][j]) % mod; } } } return C; } matrix qpow(matrix A, int m) { matrix C; for (int i = 1; i <= n + 2; i++)C.Mat[i][i] = 1; while (m) { if (m % 2)C = multi(C, A); A = multi(A, A); m >>= 1; } return C; } int main() { //ios::sync_with_stdio(false); while (cin >> n >> m) { matrix A, B; A.Mat[1][1] = 23; for (int i = 1; i <= n; i++) { // rdint(A.Mat[i + 1][1]); scanf("%lld", &A.Mat[i + 1][1]); } A.Mat[n + 2][1] = 3; for (int i = 1; i < n + 2; i++) { B.Mat[i][1] = 10; } for (int i = 1; i <= n + 2; i++) { B.Mat[i][n + 2] = 1; } for (int i = 2; i < n + 2; i++) { for (int j = 2; j <= i; j++) { B.Mat[i][j] = 1; } } B = qpow(B, m); A = multi(B, A); cout << A.Mat[n + 1][1] << endl; } }