Hdu--5015(数学,矩阵快速幂)
2014-09-15 16:20:10
思路:由于n<=10,所以用矩阵存下初始列,然后一列一列地推是可行的。
1 /************************************************************************* 2 > File Name: 5015.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sun 14 Sep 2014 11:26:51 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <queue> 14 #include <iostream> 15 #include <algorithm> 16 using namespace std; 17 typedef long long ll; 18 const int INF = 1 << 29; 19 const ll mod = 10000007; 20 21 int n,m; 22 23 struct Mx{ 24 ll a[15][15]; 25 void clear(){ 26 memset(a,0,sizeof(a)); 27 } 28 void stand(){ 29 memset(a,0,sizeof(a)); 30 for(int i = 0; i <= n + 1; ++i) 31 a[i][i] = 1; 32 } 33 Mx operator * (const Mx &b){ 34 Mx c; c.clear(); 35 for(int i = 0; i <= n + 1; ++i) 36 for(int j = 0; j <= n + 1; ++j) 37 for(int k = 0; k <= n + 1; ++k){ 38 c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % mod; 39 } 40 return c; 41 } 42 }; 43 44 Mx Mx_pow(int v){ 45 Mx res; res.stand(); 46 Mx t; t.clear(); 47 t.a[0][0] = 10; t.a[0][n + 1] = 1; 48 for(int i = 1; i <= n; ++i){ 49 for(int j = 0; j <= i; ++j){ 50 t.a[i][j] = 1; 51 } 52 } 53 t.a[n + 1][n + 1] = 1; 54 while(v){ 55 if(v & 1){ 56 res = res * t; 57 } 58 t = t * t; 59 v >>= 1; 60 } 61 return res; 62 } 63 64 int main(){ 65 while(scanf("%d%d",&n,&m) != EOF){ 66 Mx res; res.clear(); 67 res.a[0][0] = 233; 68 for(int i = 1; i <= n; ++i){ 69 scanf("%I64d",&res.a[i][0]); 70 } 71 res.a[n + 1][0] = 3; 72 res = Mx_pow(m) * res; 73 printf("%I64d\n",res.a[n][0] % mod); 74 } 75 return 0; 76 }

浙公网安备 33010602011771号