日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 5015 】233 Matrix (矩阵快速幂)

發(fā)布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 5015 】233 Matrix (矩阵快速幂) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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 a?0,1?= 233,a?0,2?= 2333,a?0,3?= 23333...) Besides, in 233 matrix, we got ai,j?= a?i-1,j?+a?i,j-1( i,j ≠ 0). Now you have known a?1,0,a?2,0,...,a?n,0, could you tell me a?n,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 ≤ 10?9). The second line contains n integers, a?1,0,a?2,0,...,a?n,0(0 ≤ a?i,0?< 2?31).

Output

For each case, output a?n,m?mod 10000007.

Sample Input

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

Sample Output

234 2799 72937

Hint

結(jié)題報告:

? ? ? ? ?依舊是按照列,找一個轉(zhuǎn)移矩陣,然后做運算,最后乘上之前保存的數(shù)組,得到想要的結(jié)果

AC代碼:

#include<bits/stdc++.h>using namespace std; const int MAX = 20 ; const int mod = 10000007 ; struct Matrix {long long mat[MAX][MAX]; }; int n,m; long long b[MAX]; Matrix mul(Matrix a,Matrix b) {Matrix c;memset(c.mat,0,sizeof(c.mat));for(int i=0; i<=n+1; i++) {for(int j=0; j<=n+1; j++) {c.mat[i][j]=0;for(int k=0; k<=n+1; k++) {if(a.mat[i][k]&&b.mat[k][j]) {c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];//矩陣乘法c.mat[i][j]%=mod;}}}}return c;//返回乘完了之后的矩陣 } Matrix q_pow(Matrix a, int k) {Matrix ans;memset(ans.mat,0,sizeof(ans.mat));for(int i=0;i<=n+1;i++)ans.mat[i][i]=1;while(k)//使用快速冪的思想進行矩陣的m次方相乘{(lán)if(k&1) {ans=mul(ans,a);}k>>=1;a=mul(a,a);}return ans; }int main() {int m;Matrix a;//轉(zhuǎn)移矩陣 while(~scanf("%d%d",&n,&m) ){//初始化第一列 //b數(shù)組就是第一列,最后與轉(zhuǎn)移矩陣的m次方相乘得到anmfor(int i=1; i<=n; i++) {scanf("%lld",&b[i]);}b[0]=23;b[n+1]=3;//求a這個轉(zhuǎn)移矩陣。先初始化!因為你比如a13 這個地方的值就沒有更新到,因為他是0,所以運算之后就可能改變值了,所以下一組輸入的時候a13這里就不是0了,所以這里一定要memset一下。 memset(a.mat,0,sizeof(a.mat));for(int i = 0; i<=n; i++) {a.mat[i][0]=10;a.mat[i][n+1]=1;}a.mat[n+1][n+1]=1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=i; j++) {a.mat[i][j]=1;}}Matrix end = q_pow(a,m);long long ans=0;for(int i = 0;i<=n+1;i++) {ans+=(end.mat[n][i]*b[i])%mod,ans%=mod;}printf("%lld\n",ans);}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的【HDU - 5015 】233 Matrix (矩阵快速幂)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。