HDU - 1575 Tr A

來源:互聯網
上載者:User

標籤:des   style   blog   資料   2014   os   

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

 22 21 00 13 999999991 2 34 5 67 8 9 
 

Sample Output

 22686 

思路:就是一道矩陣乘法快速冪

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MOD = 9973;struct Matrix {int arr[12][12];}init, unit;int n,k;Matrix Mul(Matrix a, Matrix b) {Matrix c;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {c.arr[i][j] = 0;for (int k = 0; k < n; k++)c.arr[i][j] = (c.arr[i][j]+(a.arr[i][k]*b.arr[k][j])%MOD)%MOD;}return c;}Matrix Pow(Matrix a, Matrix b, int x) {while (x) {if (x & 1)b = Mul(b, a);x >>= 1;a = Mul(a, a);}return b;}int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &k);for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {scanf("%d", &init.arr[i][j]);unit.arr[i][j] = init.arr[i][j];}Matrix res = Pow(init, unit, k-1);int ans = 0;for (int i = 0; i < n; i++) ans = (ans + res.arr[i][i]) % MOD;printf("%d\n", ans%MOD);}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.