zoukankan      html  css  js  c++  java
  • hdu 4965 矩阵快速幂 矩阵相乘性质

    Fast Matrix Calculation

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99

    Problem 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
     
    Source
     
    Recommend
    hujie   |   We have carefully selected several similar problems for you:  4970 4968 4967 4966 4964 
     
    题解:
     (4 <= N <= 1000), (2 <=K <= 6)
    N*K matrix A,K*N matrix B
    A*B是N*N,但是B*A为k*k,于是。。。
      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<map>
      9 
     10 #define N 1005
     11 #define M 15
     12 #define mod 6
     13 #define mod2 100000000
     14 #define ll long long
     15 #define maxi(a,b) (a)>(b)? (a) : (b)
     16 #define mini(a,b) (a)<(b)? (a) : (b)
     17 
     18 using namespace std;
     19 
     20 int n,k;
     21 int a[N][10],b[10][N],d[10][10],f[N][10],g[N][N],h[N][N];
     22 int ans;
     23 
     24 typedef  struct{
     25         int  m[10][10];
     26 }  Matrix;
     27 
     28 Matrix e,P;
     29 
     30 Matrix I = {1,0,0,0,0,0,0,0,0,0,
     31             0,1,0,0,0,0,0,0,0,0,
     32             0,0,1,0,0,0,0,0,0,0,
     33             0,0,0,1,0,0,0,0,0,0,
     34             0,0,0,0,1,0,0,0,0,0,
     35             0,0,0,0,0,1,0,0,0,0,
     36             0,0,0,0,0,0,1,0,0,0,
     37             0,0,0,0,0,0,0,1,0,0,
     38             0,0,0,0,0,0,0,0,1,0,
     39             0,0,0,0,0,0,0,0,0,1,
     40            };
     41 
     42 Matrix matrixmul(Matrix aa,Matrix bb)
     43 {
     44        int i,j,kk;
     45        Matrix c;
     46        for (i = 1 ; i <= k; i++)
     47            for (j = 1; j <= k;j++)
     48              {
     49                  c.m[i][j] = 0;
     50                  for (kk = 1; kk <= k; kk++)
     51                      c.m[i][j] += (aa.m[i][kk] * bb.m[kk][j])%mod;
     52                  c.m[i][j] %= mod;
     53              }
     54        return c;
     55 }
     56 
     57 Matrix quickpow(int num)
     58 {
     59        Matrix m = P, q = I;
     60        while (num >= 1)
     61        {
     62              if (num & 1)
     63                 q = matrixmul(q,m);
     64              num = num >> 1;
     65              m = matrixmul(m,m);
     66        }
     67        return q;
     68 }
     69 
     70 int main()
     71 {
     72     int i,j,o;
     73     //freopen("data.in","r",stdin);
     74     //scanf("%d",&T);
     75     //for(int cnt=1;cnt<=T;cnt++)
     76     //while(T--)
     77     while(scanf("%d%d",&n,&k)!=EOF)
     78     {
     79         if(n==0 && k==0) break;
     80         memset(d,0,sizeof(d));
     81         memset(f,0,sizeof(f));
     82         memset(g,0,sizeof(g));
     83         memset(h,0,sizeof(h));
     84         ans=0;
     85         for(i=1;i<=n;i++){
     86             for(j=1;j<=k;j++){
     87                 scanf("%d",&a[i][j]);
     88             }
     89         }
     90 
     91         for(i=1;i<=k;i++){
     92             for(j=1;j<=n;j++){
     93                 scanf("%d",&b[i][j]);
     94             }
     95         }
     96 
     97         for(i=1;i<=k;i++){
     98             for(o=1;o<=k;o++){
     99                 for(j=1;j<=n;j++){
    100                     d[i][o]+=(b[i][j]*a[j][o])%6;
    101                 }
    102                 d[i][o]%=6;
    103                 P.m[i][o]=d[i][o];
    104             }
    105         }
    106 
    107 
    108 
    109         e=quickpow(n*n-1);
    110 
    111 
    112         for(i=1;i<=n;i++){
    113             for(o=1;o<=k;o++){
    114                 for(j=1;j<=k;j++){
    115                     f[i][o]+=(a[i][j]*e.m[j][o])%6;
    116                 }
    117                 f[i][o]%=6;
    118             }
    119         }
    120 
    121         for(i=1;i<=n;i++){
    122             for(o=1;o<=n;o++){
    123                 for(j=1;j<=k;j++){
    124                     g[i][o]+=(f[i][j]*b[j][o])%6;
    125                 }
    126                 g[i][o]%=6;
    127             }
    128         }
    129 /*
    130         for(i=1;i<=n;i++){
    131             for(o=1;o<=n;o++){
    132                 for(j=1;j<=n;j++){
    133                     h[i][o]+=(g[i][j]*g[j][o])%6;
    134                 }
    135                 h[i][o]%=6;
    136             }
    137         }
    138 
    139 */
    140 
    141         for(i=1;i<=n;i++){
    142             for(o=1;o<=n;o++){
    143                 ans+=g[i][o];
    144             }
    145         }
    146         printf("%d
    ",ans);
    147 
    148     }
    149 
    150     return 0;
    151 }


     

  • 相关阅读:
    xunjian.sh
    192.168.50.235配置
    自动备份并删除旧日志
    bg和fg命令
    linux之sed用法
    正则表示第二行,第二列
    linux下redis安装
    Hma梳理
    linux 系统监控、诊断工具之 lsof 用法简介
    java的基本数据类型有八种
  • 原文地址:https://www.cnblogs.com/njczy2010/p/3922956.html
Copyright © 2011-2022 走看看