hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接。hdu 4965 Fast Matrix Calculation
题目大意:给定两个矩阵A,B,分别为N*K和K*N。
- 矩阵C = A*B
- 矩阵M=CN∗N
- 将矩阵M中的全部元素取模6,得到新矩阵M‘
- 计算矩阵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(矩阵高速幂)的更多相关文章
- HDU 4965 Fast Matrix Calculation 矩阵快速幂
题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...
- HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- 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 ...
- hdu 4965 Fast Matrix Calculation
题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...
- 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 ...
- hdu 5015 233 Matrix (矩阵高速幂)
233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- Fast Matrix Calculation 矩阵快速幂
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
随机推荐
- Android-它们的定义Notification
Android-它们的定义Notification 2014年4月26日 消息栏的消息,想必各位Android发烧友非常清楚知道是什么,比方我们下载了一个应用,它可能会定时推送些消息到我们的手机中. ...
- 将Excel数据表到数据库表
假设你有大量的数据要导入到数据库表,恐怕是没有效率的写程序,作为用于数据操纵,Excel在这方面有优势,但是,如何将其结合起来?将Excel数据表到数据库表,就是本篇博客的目的. 首先去下载MySQL ...
- ZOJ 2680 Clock()数学
主题链接:problemId=1680" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProblem ...
- Solr入门指南
本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0 因为搜索引擎功能 ...
- 深度分析 Java 的 ClassLoader 机制(源码级别)(转)
写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoa ...
- java序列化是什么和反序列化和hadoop序列化
1.什么是序列化和系列化DE- 神马是序列化它,序列化是内存中的对象状态信息,兑换字节序列以便于存储(持久化)和网络传输.(网络传输和硬盘持久化,你没有一定的手段来进行辨别这些字节序列是什么东西,有什 ...
- JavaScript获取路径
JavaScript获取路径 1.设计源代码 <%@ page language="java" import="java.util.*" pageEnco ...
- Linux下常用操作汇总
查看linux操作系统位数 (1) 终端输入: file /sbin/init 如 显示: /sbin/init: ELF 32-bit LSB executable, Intel 80386, ve ...
- Flex发行2048游戏
近来的2048像挺火的游戏.在公交车,吃.甚至还有人走在路上拿着手机在玩.之前我看我的同事们戏,我认为这是很天真,中移动太无聊了吧 到后面,他是在,我觉得真的很无聊,这时候,无聊的时候无聊,后来我想用 ...
- java.lang.ClassFormatError
Error occurred during initialization of VMjava.lang.ClassFormatError: Unknown constant tag 26 in cla ...