Fast Matrix Calculation 矩阵快速幂
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 C = A*B
C^1000 = A*(B*A)^999*B 简化到k*k上的矩阵计算方便
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 1007
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-9;
int n, k;
struct Mat
{
int a[10][10];
Mat()
{
memset(a, 0, sizeof(a));
}
Mat operator * (const Mat& rhs)const
{
Mat ret;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
if (a[i][j])
{
for (int t = 0; t < k; t++)
{
ret.a[i][t] = (ret.a[i][t] + a[i][j] * rhs.a[j][t]) % 6;
}
}
}
}
return ret;
}
};
Mat fpow(Mat a, int b)
{
Mat ret;
for (int i = 0; i < k; i++)
ret.a[i][i] = 1;
while (b != 0)
{
if (b & 1)
ret = a*ret;
a = a*a;
b /= 2;
}
return ret;
}
int m1[MAXN][7], m2[7][MAXN], ans[MAXN][MAXN], tmp[MAXN][MAXN];
int main()
{
while (scanf("%d%d", &n, &k), n + k)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
scanf("%d", &m1[i][j]);
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++)
scanf("%d", &m2[i][j]);
Mat M;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
for (int t = 0; t < n; t++)
{
M.a[i][j] = (M.a[i][j] + m2[i][t] * m1[t][j])%6;
}
}
}
M = fpow(M, n*n - 1);
memset(tmp, 0, sizeof(tmp));
memset(ans, 0, sizeof(ans));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < k; j++)
{
for (int t = 0; t < k; t++)
tmp[i][j] = (tmp[i][j] + m1[i][t] * M.a[t][j])%6;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int t = 0; t < k; t++)
ans[i][j] = (ans[i][j] + tmp[i][t] * m2[t][j])%6;
}
}
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res += ans[i][j]%6;
printf("%d\n", res);
}
}
Fast Matrix Calculation 矩阵快速幂的更多相关文章
- 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 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(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
- ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...
- bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希
题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...
- HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- 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 ...
随机推荐
- Parameterized testing with any Python test framework
1. 在进行单元测试时,很多时候需要进行参数化 尝试过使用 from nose_parameterized import parameterized 但在使用过程中会报错,后来将上面的内容改为了下面的 ...
- 大数高精度加减乘除 51nod 1005 大数加法
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...
- 2017杭电多校第七场1011Kolakoski
Kolakoski Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Tota ...
- springboot与dubbo整合入门(三种方式)
Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...
- 第八届蓝桥杯省赛C/C++ A组第8题 包子凑数
参考了http://blog.csdn.net/y1196645376/article/details/69718192 思路: 数论+完全背包. 实现: #include <iostream& ...
- JavaScript开发心得--如何传递某行数据给下一页
1, 应用场景 在某个html页面显示一批数据,如20个用户的名称.年龄等,每行都要一个编辑按钮,点击编辑后,将此行数据带入某个专门的编辑页进行显示,修改后保存. 问题是 点击编辑按钮后,如何得知要编 ...
- 主从 binlog_format 设置关系
1. 主库是row,从库必须是row/mixed.如果是statement,主库有变更时,从库报如下错误(无论什么变更都报错,如insert/update/delete/alter等): La ...
- dwarfdump --arch=arm64 --lookup
解析友盟错误信息重要指令: dwarfdump --arch=arm64 --lookup 0x1001edbc4 /Users/zhoujunbo/Library/Developer/Xcode/A ...
- 安卓app测试之Monkeyscript
MonkeyScript是一组可以被Monkey识别的命令集合 优点:MonkeyScript可以完成重复固定的操作 使用:adb shell monkey -f <scriptfile> ...
- jquery选择器,筛选器,属性,事件 基础
左边栏实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...