hdu1575---Tr A,hdu1575---Tr

來源:互聯網
上載者:User

hdu1575---Tr A,hdu1575---Tr

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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.