题目链接。hdu 4965 Fast Matrix Calculation

题目大意:给定两个矩阵A,B,分别为N*K和K*N。

  1. 矩阵C = A*B
  2. 矩阵M=CN∗N
  3. 将矩阵M中的全部元素取模6,得到新矩阵M‘
  4. 计算矩阵M’中全部元素的和

解题思路:由于矩阵C为N*N的矩阵,N最大为1000。就算用高速幂也超时,可是由于C = A*B, 所以CN∗N=ABAB…AB=AC′N∗N−1B,C‘
= B*A, 为K*K的矩阵,K最大为6。全然能够接受。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1005;
const int MOD = 6;
typedef int Mat[maxn][maxn]; int N, K;
Mat A, B, X, Y, tmp; void put (Mat x, int r, int c) {
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++)
printf("%d ", x[i][j]);
printf("\n");
}
} void mul_mat (Mat ret, Mat a, Mat b, int r, int t, int c) {
memset(tmp, 0, sizeof(tmp)); for (int k = 0; k < t; k++) {
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j]) % MOD;
}
memcpy(ret, tmp, sizeof(tmp));
} void pow_mat (Mat ret, Mat x, int n) {
memset(Y, 0, sizeof(Y));
for (int i = 0; i < K; i++)
Y[i][i] = 1; while (n) {
if (n&1)
mul_mat(Y, Y, x, K, K, K);
mul_mat(x, x, x, K, K, K);
n >>= 1;
}
memcpy(ret, Y, sizeof(Y));
} void init () {
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]);
} int main () {
while (scanf("%d%d", &N, &K) == 2 && N + K) {
init(); mul_mat(X, B, A, K, N, K); pow_mat(X, X, N*N-1); mul_mat(X, A, X, N, K, K);
mul_mat(X, X, B, N, K, N); int ans = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
ans += X[i][j];
printf("%d\n", ans);
}
return 0;
}

hdu 4965 Fast Matrix Calculation(矩阵高速幂)的更多相关文章

  1. HDU 4965 Fast Matrix Calculation 矩阵快速幂

    题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...

  2. HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律

    一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  3. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  4. HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...

  5. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  6. hdu4965 Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  7. hdu 5015 233 Matrix (矩阵高速幂)

    233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  8. Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  9. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

随机推荐

  1. php函数serialize()与unserialize() 数据序列化与反序列化

    php函数serialize()与unserialize()说明及案例.想要将已序列化的字符串变回 PHP 的值,可使用unserialize().serialize()可处理除了resource之外 ...

  2. Preview of Spring-framework :Spring框架的预习和自我整理

    Spring简介 - 预习的自我整理 1. What's Spring? Spring是一个从实际开发中抽取出来的框架,完成了大量开发中的通用步骤,留给开发者仅仅是与特定应用相关的部分,从而提高了企业 ...

  3. Java重命名文件

    File file = new File("D:\\aa\a.txt");     file.renameTo(new File("D:\\aa\\b.txt" ...

  4. ExtJs自学教程(1):从一切API开始

    称号 记得 本系列文章是不是引进全套焦点ExtJs使用,您只需专注于解决ExtJs思考问题.人们不写长篇大论.别人能学会自立.l  有些人只要学会CSS的javascript对于英文不至于很蹩脚(以辅 ...

  5. FusionCharts简单教程---建立第一个FusionCharts图形

    由于项目需求需要做一个报表,选择FusionCharts作为工具使用.由于以前没有接触过报表,网上也没有比较详细的fusionCharts教程,所以决定好好研究FusionCharts,同时做一个比较 ...

  6. 【iOS】使用SQLite与FMDB

    iOS中的SQLite与Android中的一模一样,仅仅是调用方法有差异.假设单从调用来讲,Android封装的一套helper更好用一些,而iOS原生的用C语言的几个函数在操作,比較麻烦.只是引入第 ...

  7. ios-上拉电阻负载许多其他接口

    想尝试拉加载意识到有多少开始了他的研究之旅,我看了两天做出最终的界面. 之所以这么慢是由于,我不知道要将上拉出现的view放在哪.就能在scrollView拉究竟部的时候被拉出来.还有就是怎么拉出来之 ...

  8. 191. Number of 1 Bits Leetcode Python

    Write a function that takes an unsigned integer and returns the number of '1' bits it has (also know ...

  9. ehcache.xml设置(转)

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...

  10. CSDN 夏令营课程 项目分析

    主题如以下: 正确改动后的程序: #include <iostream.h> //using namespace std; class BASE { char c; public: BAS ...