• Tr A(HDU 1575 快速矩阵幂模板)


    Tr A

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


    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
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #define MOD 9973
     6 using namespace std;
     7 struct Matrix
     8 {
     9     int mat[15][15];
    10 };
    11 int n,k,T;
    12 Matrix mul(Matrix a,Matrix b)
    13 {
    14     Matrix c;
    15     for(int i=0;i<n;i++)
    16     {
    17         for(int j=0;j<n;j++)
    18         {
    19             c.mat[i][j]=0;
    20             for(int k=0;k<n;k++)
    21                 c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD;
    22         }
    23     }
    24     return c;
    25 }
    26 Matrix mod_pow(Matrix m,int n)
    27 {
    28     Matrix res;
    29     memset(res.mat,0,sizeof(res.mat));
    30     for(int i=0;i<15;i++)
    31             res.mat[i][i]=1;
    32     while(n)
    33     {
    34         if(n&1)
    35             res=mul(res,m);
    36         m=mul(m,m);
    37         n>>=1;
    38     }
    39     return res;
    40 }
    41 int main()
    42 {
    43     freopen("in.txt","r",stdin);
    44     cin>>T;
    45     while(T--)
    46     {
    47         cin>>n>>k;
    48         int u=0;
    49         Matrix p;
    50         for(int i=0;i<n;i++)
    51             for(int j=0;j<n;j++)
    52                 cin>>p.mat[i][j];
    53         Matrix ans=mod_pow(p,k);
    54         for(int i=0;i<n;i++)
    55             u+=ans.mat[i][i];
    56         cout<<u%MOD<<endl;
    57     }
    58 }
     
  • 相关阅读:
    java线程安全单例
    ConcurrentHashMap 中putIfAbsent 和put的区别
    Google Guava -缓存cache简单使用
    Jstorm TimeCacheMap源代码分析
    poj 3277...离散化+线段树...
    spoj 1716...动态区间的最大连续子段和问题...点修改...
    spark
    hdu 1754...忘了个getchar(),蛋疼了半天...原来划水也有蛋疼的时候...
    hdu 1556...线段树划水...
    PHP学习笔记06——面向对象版图形计算器
  • 原文地址:https://www.cnblogs.com/a1225234/p/5462272.html
Copyright © 2020-2023  润新知