zoukankan      html  css  js  c++  java
  • HDU 4965 Fast Matrix Calculation (矩阵快速幂)

    Description

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.

    Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he will choose a number N (4 <= N <= 1000), and for N times, he keeps throwing his dice for K times (2 <=K <= 6) and writes down its number on the top face to make an N*K matrix A, in which each element is not less than 0 and not greater than 5. Then he does similar thing again with a bit difference: he keeps throwing his dice for N times and each time repeat it for K times to write down a K*N matrix B, in which each element is not less than 0 and not greater than 5. With the two matrix A and B formed, Alice’s task is to perform the following 4-step calculation.

    Step 1: Calculate a new N*N matrix C = A*B.
    Step 2: Calculate M = C^(N*N).
    Step 3: For each element x in M, calculate x % 6. All the remainders form a new matrix M’.
    Step 4: Calculate the sum of all the elements in M’.

    Bob just made this problem for kidding but he sees Alice taking it serious, so he also wonders what the answer is. And then Bob turn to you for help because he is not good at math.

    Input

    The input contains several test cases. Each test case starts with two integer N and K, indicating the numbers N and K described above. Then N lines follow, and each line has K integers between 0 and 5, representing matrix A. Then K lines follow, and each line has N integers between 0 and 5, representing matrix B.

    The end of input is indicated by N = K = 0.

    Output

    For each case, output the sum of all the elements in M’ in a line.

    Sample Input

    4 2
    5 5
    4 4
    5 4
    0 0
    4 2 5 5
    1 3 1 5
    6 3
    1 2 3
    0 3 0
    2 3 4
    4 3 2
    2 5 5
    0 5 0
    3 4 5 1 1 0
    5 3 2 3 3 2
    3 1 5 4 5 2
    0 0

    Sample Output

    14
    56


    题意:给定一个n*k的矩阵A和k*n的矩阵B,求A*B的n*n次幂矩阵M,并使M中的每个元素都对6取摸,然后打印出该矩阵中所有元素的和
    思路:由于矩阵A,B分别是n*k和k*n的矩阵,A*B就是一个n*n,会超过堆栈的空间,所以我们不能直接求A*B的n*n次幂。根据矩阵的结合律,可以先求B*A的n*n-1次幂,然后再右乘B,左乘A。


    代码如下:
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <algorithm>
      5 #include <cstring>
      6 #include <cmath>
      7 using namespace std;
      8 
      9 const int N=1010;
     10 
     11 int a[N][N];
     12 int b[N][N];
     13 int A[N][N];
     14 int B[N][N];
     15 
     16 struct node
     17 {
     18     int matrix[10][10];
     19     friend node operator * (node a,node b);
     20 };
     21 
     22 node M;
     23 int k;
     24 
     25 node operator * (node a,node b)
     26 {
     27     node c;    //中间矩阵
     28     for(int i=0;i<k;i++)
     29     {
     30         for(int j=0;j<k;j++)
     31         {
     32             c.matrix[i][j]=0;    //必须初始化
     33             for(int v=0;v<k;v++)
     34             {
     35                 c.matrix[i][j]=(c.matrix[i][j]+a.matrix[i][v]*b.matrix[v][j])%6;
     36             }
     37         }
     38     }
     39     return c;
     40 }
     41 
     42 node quickpow(node a,int m)
     43 {
     44     node b;     //单位矩阵
     45     for(int i=0;i<k;i++)
     46     {
     47         for(int j=0;j<k;j++)
     48         {
     49             if(i==j) b.matrix[i][j]=1;
     50             else b.matrix[i][j]=0;
     51         }
     52     }
     53     int s=m*m-1;
     54     while(s)
     55     {
     56         if(s&1) b=a*b;
     57         a=a*a;
     58         s>>=1;
     59     }
     60     return b;
     61 }
     62 
     63 int main()
     64 {
     65     int m;
     66     while(~scanf("%d%d",&m,&k))
     67     {
     68         if(m==0 && k==0) return 0;
     69         for(int i=0;i<m;i++)
     70         {
     71             for(int j=0;j<k;j++)
     72                 scanf("%d",&a[i][j]);
     73         }
     74         for(int i=0;i<k;i++)
     75         {
     76             for(int j=0;j<m;j++)
     77                 scanf("%d",&b[i][j]);
     78         }
     79         for(int i=0;i<k;i++)
     80         {
     81             for(int j=0;j<k;j++)
     82             {
     83                 M.matrix[i][j]=0;
     84                 for(int v=0;v<m;v++)
     85                 {
     86                     M.matrix[i][j]+=b[i][v]*a[v][j];
     87                     M.matrix[i][j]%=6;
     88                 }
     89             }
     90         }
     91         M=quickpow(M,m);
     92         for(int i=0;i<k;i++)
     93         {
     94             for(int j=0;j<m;j++)
     95             {
     96                 A[i][j]=0;
     97                 for(int v=0;v<k;v++)
     98                 {
     99                     A[i][j]=(A[i][j]+M.matrix[i][v]*b[v][j])%6;
    100                 }
    101             }
    102         }
    103         for(int i=0;i<m;i++)
    104         {
    105             for(int j=0;j<m;j++)
    106             {
    107                 B[i][j]=0;
    108                 for(int v=0;v<k;v++)
    109                 {
    110                     B[i][j]=(B[i][j]+a[i][v]*A[v][j])%6;
    111                 }
    112             }
    113         }
    114         long long sum=0;
    115         for(int i=0;i<m;i++)
    116         {
    117             for(int j=0;j<m;j++)
    118             {
    119                 sum+=B[i][j];
    120             }
    121         }
    122         printf("%I64d
    ",sum);
    123     }
    124     return 0;
    125 }
  • 相关阅读:
    java将string转化为int Yannis
    vm虚拟机启动报The VMware Authorization Service is not running错误 Yannis
    [org.hibernate.util.JDBCExceptionReporter] Cannot load JDBC driver class 'net. Yannis
    前台页面分页对总页数的判断 Yannis
    事务及其特性 Yannis
    iReport报表的简单函数及部分操作 Yannis
    spring aop与事务配置 Yannis
    大数据的验证和插入数据库 Yannis
    唔哇哈哈,拉霸机
    bindebug放到别的目录后不能看?编译器参数设置一下
  • 原文地址:https://www.cnblogs.com/Amidgece/p/5764915.html
Copyright © 2011-2022 走看看