九度OJ題目1443:Tr A (JAVA)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.