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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tr A(矩阵快速幂)

發布時間:2023/12/2 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tr A(矩阵快速幂) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

A為一個方陣,則Tr A表示A的跡(就是主對角線上各項的和),現要求Tr(A^k)%9973。?

Input

數據的第一行是一個T,表示有T組數據。?
每組數據的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)兩個數據。接下來有n行,每行有n個數據,每個數據的范圍是[0,9],表示方陣A的內容。?

Output

對應每組數據,輸出Tr(A^k)%9973。

Sample Input

2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9

Sample Output

2 2686

代碼:

#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<set> #include<map> #include<vector> #include<cmath>const int maxn =1e5+5; const int mod =9973; typedef long long ll; using namespace std; struct mat {int a[15][15]; }; int n; mat Mul(mat a,mat b) {mat ans;memset(ans.a,0,sizeof(ans.a));for(int t=0;t<n;t++){for(int j=0;j<n;j++){for(int k=0;k<n;k++){ans.a[t][j]=(ans.a[t][j]+a.a[t][k]*b.a[k][j])%mod;}}}return ans; } mat ans; ll quickPow(ll x) {mat res;memset(res.a,0,sizeof(res));for(int t=0;t<n;t++){res.a[t][t]=1;}while(x){if(x&1){res=Mul(ans,res);}ans=Mul(ans,ans);x>>=1;}ll ss=0;for(int t=0;t<n;t++){ss=(ss+res.a[t][t])%mod;}return ss; }int main() {int T;cin>>T;int k;while(T--){cin>>n>>k;for(int t=0;t<n;t++){for(int j=0;j<n;j++){scanf("%d",&ans.a[t][j]);}}ll s=quickPow(k);cout<<s<<endl;}return 0; }

?

轉載于:https://www.cnblogs.com/Staceyacm/p/10781745.html

總結

以上是生活随笔為你收集整理的Tr A(矩阵快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。