`
Simone_chou
  • 浏览: 192672 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Fast Matrix Calculation(矩阵快速幂)

    博客分类:
  • HDOJ
 
阅读更多

Fast Matrix Calculation

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


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

 

      题意:

      给出 N(4 ~ 1000) 和 K(2 ~ 6),后给出两个矩阵 A (N X K)和 B (K X N),令 C = A X B,求出 C 的 N * N 次方矩阵,输出矩阵所有数的和(矩阵里面的数要 % 6)。

 

      思路:

      因为 (A X B) ^ ( N * N ),所以可以化成 A X (A X B) ^ ( N x N - 1) X B,这样的话 K 只是 2 ~ 6,就不会造成 TLE。最后矩阵快速幂即可。

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;

mat mul(mat a, mat b) {
    mat c(a.size(), vec(b[0].size()));

    for (int i = 0; i < a.size(); ++i) {
        for (int j = 0; j < b[0].size(); ++j) {
            for (int k = 0; k < b.size(); ++k) {
                c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % 6;
            }
        }
    }

    return c;
}

mat pow(mat a, int n) {
    mat b(a.size(), vec(a[0].size()));
    for (int i = 0; i < a.size(); ++i)
        b[i][i] = 1;

    while (n > 0) {
        if (n & 1) b = mul(b, a);
        a = mul(a, a);
        n >>= 1;
    }

    return b;
}

int main() {

    int n, k;

    while (~scanf("%d%d", &n, &k) && (n + k)) {
        mat a(n, vec(k));
        mat b(k, vec(n));
        mat c(k, vec(k));

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < k; ++j) {
                scanf("%d", &a[i][j]);
            }
        }

        for (int i = 0; i < k; ++i) {
            for (int j = 0; j < n; ++j) {
                scanf("%d", &b[i][j]);
            }
        }

        c = mul(b, a);
        c = pow(c, n * n - 1);
        c = mul(a, c);
        c = mul(c, b);

        int sum = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                sum += c[i][j];
            }
        }

        printf("%d\n", sum);
    }

    return 0;
}

 

 

分享到:
评论

相关推荐

    matrix_calculation.rar_矩阵运算

    "matrix_calculation.rar_矩阵运算"这个压缩包文件显然是一个关于矩阵运算的详细资源,其中包含了一个名为"matrix_calculation.doc"的文档。以下是对矩阵运算的详细介绍。 矩阵是由有序数组构成的矩形阵列,其元素...

    Matrix Calculation

    3. 上三角矩阵(Upper Triangular Matrix)和下三角矩阵(Lower Triangular Matrix):分别指主对角线以下和以上的元素为零的矩阵。 4. 对称矩阵(Symmetric Matrix):矩阵与其转置相等,即A = AT。 5. 奇异矩阵...

    Fast Calculation Method for Supercritical CO2 Thermodynamic Properties

    标题中提到的"Fast Calculation Method for Supercritical CO2 Thermodynamic Properties"揭示了当前科学领域对于超临界二氧化碳热力学性质快速计算方法的研究需求。超临界流体在许多工业过程中扮演着重要角色,尤其...

    Extension of The Implicit Curve-Fitting Method for Fast Calculation of Thermodynamic Properties to Subcooled Refrigerant

    Extension of The Implicit Curve-Fitting Method for Fast Calculation of Thermodynamic Properties to Subcooled Refrigerant,丁国良,吴志刚,Calculations of refrigerant thermal properties are desired to ...

    Matrix_Eigenvalue_calculation.rar_conditionyir_eigenvalue

    矩阵特征值计算 Matrix Eigenvalue calculation

    matlab matrix Eigenvalue calculation.rar_eigenvalue_eigenvalue

    矩阵特征值计算,不同精度,不同迭代步数,收敛速度计算

    exposition fast calculation with ARM NEON

    本文将深入探讨如何利用ARM NEON技术进行快速的图像曝光计算,帮助开发者实现更优化的性能。 ARM NEON是ARM公司推出的一种向量并行计算单元,专门用于提升处理器在媒体处理、图像和信号处理等领域的性能。它是一种...

    Simple Calculation

    "Simple Calculation"是一款基于C#编程语言开发的计算器软件,专为用户提供强大的计算功能,适合在实际应用中使用,同时也适合开发者进行技术交流和学习。本文将深入探讨这款计算器的开发背景、主要特点、核心技术和...

    Hessenberg_calculation.zip_Hessenberg矩阵计算_hessenberg

    `Hessenberg_calculation.m` 文件很可能是一个MATLAB脚本,用于实现Hessenberg矩阵的相关计算。MATLAB是一种强大的数值计算和数据可视化环境,非常适合处理此类矩阵运算。这个脚本可能包含了将一般矩阵转换为...

    Fast calculation of bistatic RCS for conducting objects using the adaptive cross approximation algorithm

    Adaptive cross approximation (ACA) algorithm as a kind of common matrix compression method is often used to analyze the electromagnetic radiation and scattering problems. In the region of far field, ...

    Java计算器程序Calculation

    这个名为"Calculation"的程序利用了Java Swing库来构建图形用户界面(GUI),为用户提供一个直观的方式来执行基本的数学运算。Swing是Java Foundation Classes (JFC)的一部分,提供了丰富的组件集,用于创建美观且...

    Matlab calculation_matlab_metal6y7_matlabbasic_

    Matlab,全称Matrix Laboratory,是一款强大的数值计算和符号计算软件,广泛应用于工程、科学、经济等领域。它以其简洁的编程语法和丰富的数学函数库,使得复杂的数学计算变得简单易行。在"Matlab calculation_...

    master_calculation_

    在IT领域,编程是实现各种功能的基础,"master_calculation_"这个标题暗示了我们正在讨论一个关于计算器程序的项目。这个项目可能是一个自定义的、更高级的计算器,旨在提供比标准图形用户界面(GUI)计算器更多的...

    Matrix-Calculation:一个 Java 程序,用于对从文件中读取的矩阵执行基本操作。 CS213高级编程-Lab01

    矩阵计算 这个项目是一个矩阵计算器。 它接受输入的多个矩阵并对它们执行基本操作,如矩阵加法、减法和除法。 作者:古拉姆·乌尔·哈桑·阿里。 NUST - SEECS 日期:2015 年 2 月 18 日 - 2015 年 该程序可以免费...

    calculation_java_

    在本项目"calculation_java_"中,我们主要探讨的是如何使用Java编程语言来实现一个基本的加减乘除运算器。这个课程设计旨在帮助学习者深入理解Java编程基础,特别是对象导向编程(OOP)的概念,同时提升算法设计和...

    matrix_randomcode_Able_

    描述“matrix calculation able to generate random code”表明我们要讨论的是一种能够生成随机代码的矩阵计算方法。随机代码通常在软件测试、加密算法或模拟实验中扮演重要角色。它可能涉及到生成随机数值矩阵,...

    EQ_Calculation_equalizer_

    标题中的“EQ_Calculation_equalizer_”暗示了这是一个关于音效处理的项目,特别是均衡器(Equalizer)的计算部分。均衡器是一种音频信号处理工具,用于调整不同频率的声音强度,以改善音质或适应特定听音环境。 在...

    poj 2827 Auto-Calculation Machine.md

    poj 2827 Auto-Calculation Machine.md

    中英文_Calculation-Manager-for-Hyperion-Planning 11.1.1

    《中英文_Calculation-Manager-for-Hyperion-Planning 11.1.1》这份资料专注于Oracle Hyperion Planning中的Calculation Manager(计算管理器)的业务规则应用,旨在帮助用户理解和掌握这一强大的工具。Oracle ...

    资料-flyback transformer calculation.zip

    在“资料-flyback transformer calculation.xls”这份计算表中,用户可以输入上述参数,通过内置的公式和计算功能,快速得到反激变压器的设计结果,如:绕组电流、磁芯面积、平均磁通密度、电感值、漏感值等。...

Global site tag (gtag.js) - Google Analytics