HDU - 1575 Tr A

Tr A

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5824    Accepted Submission(s): 4383


Problem Description
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 <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#define max_ 200010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;

struct mat
{
    ll num[20][20];
    int n;
}a;
mat mul(struct mat a,struct mat b)
{
    struct mat ans;
    ans.n=a.n;
    memset(ans.num,0,sizeof(ans.num));
    for(int i=1;i<=a.n;i++)
    {
        for(int j=1;j<=a.n;j++)
        {
            for(int k=1;k<=a.n;k++)
            {
                ans.num[i][j]+=(a.num[i][k]*b.num[k][j])%9973;
                ans.num[i][j]%=9973;
            }
        }
    }
    return ans;
}
void show(struct mat a)
{
    printf("%d\n",a.n);
    for(int i=1;i<=a.n;i++)
    {
        for(int j=1;j<=a.n;j++)
        {
            printf("%d ",a.num[i][j]);
        }
        printf("\n" );
    }
}
int fpow(struct mat a,int k)
{
    struct mat ans,tmp=a;
    ans.n=a.n;
    for(int i=1;i<=a.n;i++)
    {
        for(int j=1;j<=a.n;j++)
        {
            if(i==j)
            ans.num[i][j]=1;
            else
            ans.num[i][j]=0;
        }
    }
    while(k!=0)
    {
        if(k&1)
            ans=mul(ans,tmp);
        tmp=mul(tmp,tmp);
        k/=2;
    }
    // show(ans);
    ll sum=0;
    for(int i=1;i<=ans.n;i++)
    {
        sum=(sum+ans.num[i][i])%9973;
    }
    return (sum%9973);
}
int main(int argc, char const *argv[]) {
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        a.n=n;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>a.num[i][j];
            }
        }
        printf("%d\n",fpow(a,k));
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值