zoukankan      html  css  js  c++  java
  • hdoj 1575 Tr A 矩阵快速幂

    Tr A

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    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
    思路:裸题,直接套模板。。
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 const int mod = 9973;
     5 using namespace std;
     6 int N;
     7 struct mat {
     8     int a[10][10];
     9     mat() {
    10         memset(a, 0, sizeof(a));
    11     }
    12     mat operator *(const mat &o) const {
    13         mat t;
    14         for(int i = 0; i < N; i++) {
    15             for(int j = 0; j < N; j++)
    16                 for(int k = 0; k < N; k++) {
    17                     t.a[i][j] = (t.a[i][j] + a[i][k]*o.a[k][j])%mod;
    18                 }
    19         }
    20         return t;
    21     }
    22 } a, b;
    23 
    24 int main() {
    25     int n, t;
    26     scanf("%d", &t);
    27     while(t--) {
    28         scanf("%d%d", &N, &n);
    29         mat a, b;
    30         for(int i = 0; i < N; i++) a.a[i][i] = 1;
    31         for(int i = 0; i < N; i++) {
    32             for(int j = 0; j < N; j++)
    33                 scanf("%d", &b.a[i][j]);
    34         }
    35         while(n > 0) {
    36             if(n&1) {
    37                 a = b*a;
    38                 n--;
    39             }
    40             n >>= 1;
    41             b = b*b;
    42         }
    43         int ans = 0;
    44         for(int i = 0; i < N; i++)
    45             ans = (ans + a.a[i][i])%mod;
    46         printf("%d
    ", ans);
    47     }
    48 
    49     return 0;
    50 }
     
  • 相关阅读:
    cocos2dx CCSprite自动拉伸全屏
    linux 安装输入法
    linux jdk 配置
    Proguard.cfg 配置
    C++基本概念
    查看android keystore 别名
    view onTouch,onClick,onLongClick
    LiteDB V4.1.4版本 查询日期写法 C#
    解决Highcharts 5.0.7,IE8下bar类型图表无法显示的问题
    AspNetCore AmbiguousMatchException: The request matched multiple endpoints. Matches
  • 原文地址:https://www.cnblogs.com/cshg/p/5918734.html
Copyright © 2011-2022 走看看