标签:init color nbsp other span cstring ios i++ str
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4604 Accepted Submission(s): 3461
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<string.h> #include<map> #include<set> #include<queue> #include<vector> #include<cstdlib> typedef long long ll; typedef unsigned long long LL; using namespace std; const int INF=0x3f3f3f3f; const int num=100; const int mod=9973; int N; struct Mat{ int a[num][num]; void init(){ memset(a,0,sizeof(a)); for(int i=0;i<num;i++) a[i][i]=1; } }; //矩阵加法 Mat add(Mat a,Mat b){ Mat ans; for(int i=0;i<N;i++) for(int j=0;j<N;j++){ ans.a[i][j]=a.a[i][j]+b.a[i][j]; ans.a[i][j]=ans.a[i][j]%mod; } return ans; } //矩阵乘法 Mat mul(Mat a,Mat b){ Mat ans; for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ ans.a[i][j]=0; for(int k=0;k<N;k++){ ans.a[i][j]+=a.a[i][k]*b.a[k][j]; } ans.a[i][j]=ans.a[i][j]%mod; } } return ans; } //矩阵快速幂 Mat power(Mat a,int n){ Mat ans; ans.init(); while(n){ if(n&1){ ans=mul(ans,a); } n=n>>1; a=mul(a,a); } return ans; } //矩阵的幂和 Mat pow_sum(Mat a,int n){ int m; Mat ans,pre; if(n==1){ return a; } m=n/2; pre=pow_sum(a,m); ans=add(pre,mul(pre,power(a,m))); if(n&1) ans=add(ans,power(a,n)); return ans; } void output(Mat a){ for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ if(j==0)printf("%d",a.a[i][j]); else printf(" %d",a.a[i][j]); } printf("\n"); } } int main(){ int tt; int k,n; scanf("%d",&tt); while(tt--){ int t=0; scanf("%d%d",&n,&k); Mat a; N=n; for(int i=0;i<N;i++){ for(int j=0;j<N;j++) scanf("%d",&a.a[i][j]); } Mat ans=power(a,k); //output(ans); for(int i=0;i<N;i++){ t=(t+ans.a[i][i])%mod; } printf("%d\n",t); } return 0; }
标签:init color nbsp other span cstring ios i++ str
原文地址:http://www.cnblogs.com/Aa1039510121/p/6120261.html