1 solutions

  • 0
    @ 2022-1-20 20:13:16
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    ll n,k;
    struct pre{
    	ll c[103][103];
    }A,I; 
    pre operator*(const pre &x,const pre &y){
    	pre a;
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			a.c[i][j]=0;
    		}
    	}
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			for(int k=1;k<=n;k++){
    				a.c[i][j]+=x.c[i][k]*y.c[k][j]%mod;
    				a.c[i][j]%=mod;
    			}
    		}
    	}
    	return a;
    }
    int main(){
    	cin>>n>>k;
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			scanf("%lld",&A.c[i][j]);
    		}
    	}
    	for(int i=1;i<=n;i++){
    		I.c[i][i]=1;
    	}
    	while(k>0){
    		if(k&1) I=I*A;//注意顺序,十分致命 
    		A=A*A;
    		k=k>>1;
    	}
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			cout<<I.c[i][j]<<" ";
    		}
    		printf("\n");
    	}
    	return 0;
    }
    
    • 1

    Information

    ID
    331
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    4
    Tags
    # Submissions
    59
    Accepted
    15
    Uploaded By