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): 87    Accepted Submission(s): 39


    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
    思路:
    C = A * B, M = C^(n*n),这题我的第一反应就是矩阵乘法啊。。但是却直接把A * B求了出来,然后计算C的n×n次幂
    而这题解题的关键就在这里,因为A是n×k的矩阵,B是k×n的矩阵,而n很大,k很小。
    所以可以把M写成 M = A*(B*A)*(B*A)*....*(B*A)*B
    这样就这样用快速幂求得(B×A)^(n*n-1),然后右乘一个B,再左乘一个A即可。智商真是拙计。!!
    Accepted Code:
     1 /*************************************************************************
     2     > File Name: 1006.cpp
     3     > Author: Stomach_ache
     4     > Mail: sudaweitong@gmail.com
     5     > Created Time: 2014年08月19日 星期二 12时05分28秒
     6     > Propose: 
     7  ************************************************************************/
     8 
     9 #include <cmath>
    10 #include <string>
    11 #include <cstdio>
    12 #include <fstream>
    13 #include <vector>
    14 #include <cstring>
    15 #include <iostream>
    16 #include <algorithm>
    17 using namespace std;
    18 /*Let's fight!!!*/
    19 
    20 struct mat {
    21   int n, m;
    22   vector<vector<int> >M;
    23   mat() {}
    24   mat(int a, int b):n(a), m(b) {
    25         M.resize(n);
    26         for (int i = 0; i < n; i++) M[i].resize(m, 0);
    27   }
    28   friend mat operator * (const mat &a, const mat &b) {
    29      mat c(a.n, b.m);
    30      for (int i = 0; i < a.n; i++) {
    31          for (int k = 0; k < b.n; k++) {
    32              for (int j = 0; j < b.m; j++) {
    33                  c.M[i][j] += a.M[i][k] * b.M[k][j];      //注意这里的循环顺序
    34                  c.M[i][j] %= 6;
    35              }
    36          }
    37      }
    38      return c;
    39   }
    40 };
    41 void read(int &res) {
    42       res = 0; char c = ' ';
    43     while (c < '0' || c > '9') c = getchar();
    44     while (c >= '0'&&c<='9') res = res*10+c-'0', c = getchar();
    45 }
    46 
    47 int main(void) {
    48       int n, k;
    49     while (~scanf("%d %d", &n, &k) && n + k) {
    50           mat A(n, k), B(k, n);
    51           for (int i = 0; i < n; i++) for (int j = 0; j < k; j++)    
    52               read(A.M[i][j]);
    53           for (int i = 0; i < k; i++) for (int j = 0; j < n; j++)    
    54               read(B.M[i][j]);
    55         mat C = B * A;
    56         int b = n * n - 1;
    57         mat res(k, k);
    58         for (int i = 0; i < k; i++) res.M[i][i] = 1;
    59         while (b) {
    60               if (b&1) res = res * C;
    61             C = C * C;
    62             b >>= 1;
    63         }
    64         mat ans = A * (res * B);
    65         int sum = 0;
    66         for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) 
    67               sum += ans.M[i][j];
    68         printf("%d
    ", sum);
    69     }
    70     
    71     return 0;
    72 }
  • 相关阅读:
    4、redis之使用commons-pool
    好用的eclipse properties插件
    1、redis之安装与配置
    谷歌浏览器禁止window.close的问题
    在浏览器中使用JS打开并展示PDF文件
    JAVA遍历Map的方法
    GEOS库学习之三:空间关系、DE-9IM和谓词
    GEOS库的学习之二:简单几何图形的创建
    GEOS库的学习之一:介绍和编译
    GEOS库在windows中的编译和测试(vs2012)
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3922784.html
Copyright © 2011-2022 走看看