標籤:des ati pac 位元組 rgs string cin class 輸入
-
題目描述:
-
A為一個方陣,則Tr A表示A的跡(就是主對角線上各項的和),現要求Tr(A^k)%9973。
-
輸入:
-
資料的第一行是一個T,表示有T組資料。
每組資料的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)兩個資料。接下來有n行,每行有n個資料,每個資料的範圍是[0,9],表示方陣A的內容。
-
輸出:
-
對應每組資料,輸出Tr(A^k)%9973。
-
範例輸入:
-
22 21 00 13 999999991 2 34 5 67 8 9
-
範例輸出:
-
22686
1 package a; 2 import java.util.*; 3 public class Main{ 4 public static void main(String[] args) { 5 Scanner cin = new Scanner(System.in); 6 while(cin.hasNext()){ 7 int t=cin.nextInt(); 8 while(t!=0){ 9 int count=0;10 int n=cin.nextInt();11 int k=cin.nextInt();12 int a[][]=new int[n][n];13 int c[][]=new int[n][n];14 for(int i=0;i<n;i++){15 for(int j=0;j<n;j++){16 a[i][j]=cin.nextInt();17 if(i==j)18 c[i][j]=1;//初始化成單位元組。19 else c[i][j]=0; 20 }21 } 22 while(k!=0) {//二分法求冪。23 if(k%2 == 1)24 c = fuc(a, c);25 k /= 2;26 a = fuc(a, a);27 }28 for(int i=0;i<n;i++){29 count+=c[i][i];30 count%=9973;31 32 }33 System.out.println(count);34 t--;35 }36 }37 cin.close(); 38 } 39 public static int[][] fuc(int a[][],int b[][]){40 int c[][]=new int[a.length][a.length];41 for(int i=0;i<c.length;i++){//矩陣乘法三個迴圈。42 for(int j=0;j<c.length;j++){43 for(int k=0;k<c.length;k++){44 c[i][j]+=a[i][k]*b[k][j];//別忘了這個加號。45 c[i][j]%=9973; 46 }47 }48 }49 return c;50 } 51 }
九度OJ題目1443:Tr A (JAVA)