Tr A(矩阵快速幂)
生活随笔
收集整理的這篇文章主要介紹了
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 9Sample 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(矩阵快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 凯隐战士符文(凯隐蓝凯符文)
- 下一篇: Error:The supplied j