Fast Matrix Calculation HDU - 4965
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.
InputThe 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.OutputFor 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*m和一个m*n的矩阵,经过上面的4步之后会得到一个新的矩阵M,求M中所有元素的总和。
n是一个可以到1000的数,但是m巨小,最多到6,矩阵开1000会爆栈,我们可以转化一下:
A*B^(n*n) = A*B*A*B*A*B...*A*B = A*(B*A)^(n*n-1)*B
B*A是一个m*m的矩阵,嘿嘿~~~
//Asimple
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define test() cout<<"============"<<endl
#define debug(a) cout << #a << " = " << a <<endl
#define dobug(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
using namespace std;
typedef long long ll;
const int N = ;
const ll MOD=;
const int INF = ( << );
const double PI=atan(1.0)*;
const int maxn = +;
const ll mod = ;
ll n, m, len, ans, sum, v, w, T, num;
int A[maxn][maxn], B[maxn][maxn];
int c1[maxn][maxn], c2[maxn][maxn]; struct Matrix {
long long grid[N][N];
int row,col;
Matrix():row(N),col(N) {
memset(grid, , sizeof grid);
}
Matrix(int row, int col):row(row),col(col) {
memset(grid, , sizeof grid);
} //矩阵乘法
Matrix operator *(const Matrix &b) {
Matrix res(row, b.col);
for(int i = ; i<res.row; i++)
for(int j = ; j<res.col; j++)
for(int k = ;k<col; k++)
res[i][j] = (res[i][j] + grid[i][k] * b.grid[k][j] + MOD) % MOD;
return res;
} //矩阵快速幂
Matrix operator ^(long long exp) {
Matrix res(row, col);
for(int i = ; i < row; i++)
res[i][i] = ;
Matrix temp = *this;
for(; exp > ; exp >>= , temp = temp * temp)
if(exp & ) res = temp * res;
return res;
} long long* operator[](int index) {
return grid[index];
} void print() {
for(int i = ; i <row; i++) {
for(int j = ; j < col-; j++)
printf("%d ",grid[i][j]);
printf("%d\n",grid[i][col-]);
}
}
}; void input(){
ios_base::sync_with_stdio(false);
while( cin >> n >> m && (n+m) ) {
for(int i=; i<n; i++)
for(int j=; j<m; j++)
cin >> A[i][j];
for(int i=; i<m; i++)
for(int j=; j<n; j++)
cin >> B[i][j];
Matrix C(m, m);
for(int i=; i<m; i++) {
for(int j=; j<m; j++) {
C[i][j] = ;
for(int k=; k<n; k++) {
C[i][j] += ( B[i][k]*A[k][j]);
C[i][j] %= ;
}
}
}
C = C^(n*n-); for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
c1[i][j] = ;
for(int k=; k<m; k++) {
c1[i][j] += A[i][k]*C[k][j];
c1[i][j] %= ;
}
}
}
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
c2[i][j] = ;
for(int k=; k<m; k++) {
c2[i][j] += c1[i][k]*B[k][j];
}
}
} ans = ;
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
ans += (c2[i][j]%MOD);
}
}
cout << ans << endl;
}
} int main(){
input();
return ;
}
Fast Matrix Calculation HDU - 4965的更多相关文章
- hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
- hdu 4965 Fast Matrix Calculation
题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...
- 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 矩阵快速幂
题意: 给出一个\(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 ...
- 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 ...
随机推荐
- Axure RP 9 Beta 开放下载(更新激活密钥和汉化包)
2018年9月9号,7月9号来厦门入职,已经两个月了.这两个月的生活状态真心不好,一方面工作很忙(刚工作是这样?),虽然工资还可以,但总感觉性价比很低,自已对这份工作不够热爱也许.另一方面,来到新城市 ...
- Oracle同义词(synonym)
oracle的同义词总结 从字面上理解就是别名的意思,和视图的功能类似.就是一种映射关系. 同义词拥有如下好处: 节省大量的数据库空间,对不同用户的操作同一张表没有多少差别; 扩展的数 ...
- 理解vue之element-ui中的 <template slot-scope="scope">
https://blog.csdn.net/tg928600774/article/details/81945140?utm_source=blogxgwz1
- Java课程寒假之《人月神话》有感之一
一.焦油坑 以前上课的时候,老师讲过早期的程序由于工作量不大,大多只需要几个人完成,随着软件规模的不断扩大,代码量直线上升,仅仅一两个人可能没有办法完成这样的任务,多以开始形成了团队的规模,焦油坑说的 ...
- python 链表表达式 map、filter易读版
链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...
- C语言函数strstr
函数原型: extern char *strstr(char *str1, const char *str2); 语法: * strstr(str1,str2) 参数: str1: 被查找目标 ...
- Java编程基础篇第六章
构造方法 一:概念: 给对象的数据(属性)进行初始化 二:特点: a.方法名与类同名(字母大小写也要一样) b.没有返回值类型 c.没有具体的返回值 return 三:构造方法重载: 方法名相同,与返 ...
- mysql避免数据库误操作小技巧(转)
避免混淆开发环境的DB和生产环境的DB这在小公司小团队尤其常见.一个人即负责开发,又管DB.桌面上开了一坨终端,有的是开发的DB,有的是生产的DB.一不留神,就写串了,或者粘贴串了.更郁闷的是,有时候 ...
- 解决键盘输入被JDB占用的问题
解决键盘输入被JDB占用的问题 本周的任务"迭代和JDB"在使用JDB调试时需要键盘输入数据,但我在正确的位置输入数据后发现JDB提示如图所示的错误. 上网查找后得知该错误的产生是 ...
- java框架之SpringCloud(5)-Hystrix服务熔断、降级与监控
前言 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败.不做任何处理的情况下,很容易导致服务雪崩. 服务雪崩:多个微服务之间调用的时候,假设 ...