hdu1575---Tr A

来源:互联网 发布:图片文件格式软件安装 编辑:程序博客网 时间:2024/05/22 13:31

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

Author
xhd

Source
HDU 2007-1 Programming Contest

Recommend
linle | We have carefully selected several similar problems for you: 1757 1588 2256 2604 2254

水题,矩阵快速幂

/*************************************************************************    > File Name: hdu1575.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年03月10日 星期二 19时20分38秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double pi = acos(-1);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;const int mod = 9973;int n;struct MARTIX{    int mat[15][15];};MARTIX mul (MARTIX a, MARTIX b){    MARTIX c;    for (int i = 0; i < n; ++i)    {        for (int j = 0; j < n; ++j)        {            c.mat[i][j] = 0;            for (int k = 0; k < n; ++k)            {                c.mat[i][j] += a.mat[i][k] * b.mat[k][j];                c.mat[i][j] %= mod;            }        }    }    return c;}void fastpow (MARTIX ret, int k){    MARTIX ans;    for (int i = 0; i < n; ++i)    {        for (int j = 0; j < n; ++j)        {            ans.mat[i][j] = (i == j);        }    }    while (k)    {        if (k & 1)        {            ans = mul (ans, ret);        }        ret = mul (ret, ret);        k >>= 1;    }    int res = 0;    for (int i = 0; i < n; ++i)    {        res += ans.mat[i][i];        res %= mod;    }    printf("%d\n", res);}int main (){    int t;    scanf("%d", &t);    while (t--)    {        int k;        scanf("%d%d", &n, &k);        MARTIX a;        for (int i = 0; i < n; ++i)        {            for (int j = 0; j < n; ++j)            {                scanf("%d", &a.mat[i][j]);            }        }        fastpow(a, k);    }    return 0;}
0 0
原创粉丝点击