• HDU 1575 Tr A 矩阵快速幂


    题意:求矩阵经过n次自乘以后对角线的和

    解题思路:裸矩阵快速幂。

    解题代码:

      1 // File Name: temp.cpp
      2 // Author: darkdream
      3 // Created Time: 2014年09月17日 星期三 11时35分45秒
      4 
      5 #include<vector>
      6 #include<list>
      7 #include<map>
      8 #include<set>
      9 #include<deque>
     10 #include<stack>
     11 #include<bitset>
     12 #include<algorithm>
     13 #include<functional>
     14 #include<numeric>
     15 #include<utility>
     16 #include<sstream>
     17 #include<iostream>
     18 #include<iomanip>
     19 #include<cstdio>
     20 #include<cmath>
     21 #include<cstdlib>
     22 #include<cstring>
     23 #include<ctime>
     24 #define LL long long
     25 #define m 9973
     26 using namespace std;
     27 int n , k; 
     28 struct Matrix
     29 {
     30    int  mat[20][20];
     31    void clear()
     32    {
     33       memset(mat,0,sizeof(mat));
     34    }
     35    void output()
     36    {
     37      for(int i =0  ;i < n ;i ++)
     38      {
     39        for(int j = 0 ;j < n ;j ++)
     40            printf("%d ",mat[i][j]);
     41      printf("
    ");
     42      }
     43    }
     44    void init()
     45    {
     46       for(int i = 0;i < n ;i ++) 
     47           for(int j = 0 ;j < n;j ++)
     48           {
     49               scanf("%d",&mat[i][j]);
     50           }
     51    }
     52    Matrix operator *(const Matrix &b) const
     53    {
     54        Matrix ret;
     55        ret.clear();
     56        for(int i = 0 ;i < n ;i ++)
     57            for(int j = 0;j < n;j ++)
     58            {
     59                for(int k = 0 ;k < n ;k ++)
     60                {
     61                  ret.mat[i][j] =(ret.mat[i][j] + mat[i][k] * b.mat[k][j]) % m ; // 第I 行  第J  列        
     62                }
     63            }
     64        return ret;
     65    }
     66 };
     67 Matrix Pow( Matrix a ,int t )
     68 {
     69   Matrix ret;
     70   ret.clear();
     71   for(int i = 0 ;i < n ;i ++)
     72        ret.mat[i][i] = 1;
     73   Matrix tmp = a; 
     74   while(t)
     75   {
     76       if(t&1) ret = ret * tmp;
     77       tmp = tmp * tmp;
     78       t >>=1;
     79   }
     80   return ret;
     81 }
     82 int main(){
     83     int t; 
     84     scanf("%d",&t);
     85     while(t--)
     86     {
     87          scanf("%d %d",&n,&k);
     88          Matrix  a;
     89          a.init();
     90          //a.output();
     91          a = Pow(a,k);
     92          //a.output();
     93          int  ans = 0 ;
     94          for(int i = 0 ;i < n;i ++)
     95          {
     96             ans =(ans + a.mat[i][i]) % m ;
     97          }
     98       printf("%d
    ",ans);
     99     }
    100 return 0;
    101 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    函数的定义
    函数名的本质
    函数进阶
    三元运算
    数据类型 补充
    安装python问题
    neo4j nosql图数据库学习
    ubutun lunix 64安装neo4j 图形数据库
    git error: object file .git/objects/b9/e269f50db2a3415cc8ad5ba40b82b9b6a13d45 is empty
    django orm 时间处理
  • 原文地址:https://www.cnblogs.com/zyue/p/3977188.html
Copyright © 2020-2023  润新知