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.
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 题意 :按照题目所给的要求,求最终答案
思路 : 在一个结构体中去存一个二维矩阵时,最多可以开到 f[800][800],在往大就会直接停止运行了。
解决此问题有一个关键的地方就是 A*B^(n*n), 这样求的话 A*B 就是1000*1000的矩阵,指定是 超时,如何展开 A*B*A*B*A*B……A*B,等于 A*(B*A)^(n*n-1),转变成为了一个 6 * 6 的矩阵。 代码 :
int a[1005][10];
int b[10][1005];
struct mat
{
int a[6][6];
};
int k;
int c[1005][10]; mat mul(mat a, mat b){
mat r;
memset(r.a, 0, sizeof(r.a)); for(int i = 0; i < k; i++){
for(int f = 0; f < k; f++){
if(a.a[i][f]){
for(int j = 0; j < k; j++){
if(b.a[f][j]){
r.a[i][j] += a.a[i][f]*b.a[f][j];
r.a[i][j] %= 6;
}
}
}
}
}
return r;
} mat pow(mat a, int n){
mat b;
memset(b.a, 0, sizeof(b.a));
for(int i = 0; i < k; i++) b.a[i][i] = 1; while(n){
if(1 & n) b = mul(b, a);
a = mul(a, a);
n >>= 1;
}
return b;
} int main() {
int n; while(~scanf("%d%d", &n, &k)&& n+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]);
}
}
mat A;
memset(A.a, 0, sizeof(A.a));
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
for(int f = 0; f < n; f++){
A.a[i][j] += b[i][f]*a[f][j];
A.a[i][j] %= 6;
}
}
}
A = pow(A, n*n-1);
memset(c, 0, sizeof(c));
for(int i = 0; i < n; i++){
for(int j = 0; j < k; j++){
for(int f = 0; f < k; f++){
c[i][j] += a[i][f]*A.a[f][j];
c[i][j] %= 6;
}
}
}
int sum = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int t = 0;
for(int f = 0; f < k; f++){
t += c[i][f]*b[f][j];
t %= 6;
}
sum += t;
}
}
printf("%d\n", sum);
} return 0;
}
hdu - 4965的更多相关文章
- 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(矩阵高速幂)
题目链接.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次,这样中间的矩阵一个 ...
- 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 矩阵快速幂
顺手写了下矩阵类模板 利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B #include <cstdio> #include <cstring> ...
- Fast Matrix Calculation HDU - 4965
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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Jav ...
- HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- Hdu 4965(矩阵快速幂)
题目链接 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
随机推荐
- python模块之configparser模块
configparser模块:用于按一定格式创建配置文件 创建 import configparser config = configparser.ConfigParser() config['DEF ...
- VisualStudio 扩展开发 获得输出窗口内容
本文告诉大家如何拿到 VisualStudio 输出窗口的内容 在上一篇告诉大家如何开发添加菜单 点击的时候可以使用方法,如果需要拿到 VisualStudio 的输出窗口的内容,如想要开发一个插件, ...
- vue-上传文件
<label for="exampleInputFile">头像</label> <img :src=" imgsrc != '' ? im ...
- printk函数 打印设备编号
偶尔地, 当从一个驱动打印消息, 你会想打印与感兴趣的硬件相关联的设备号. 打印主次 编号不是特别难, 但是, 为一致性考虑, 内核提供了一些实用的宏定义( 在 <linux/kdev_t.h& ...
- Command failed: git -c core.longpaths=true config --get remote.origin.url
「Unable to Connect to GitHub.com For Cloning」 Error: Command failed: git -c core.longpaths=true conf ...
- [转载]sublime用法精华
Sublime Text 全程指南 九月 03.2015. 暂无评论 永远站 作者:Lucida 原文链接:http://lucida.me/blog/sublime-text-complete-gu ...
- 【土旦】vue 解决ios H5底部输入框 获取焦点时弹出虚拟键盘挡住输入框 以及监听键盘收起事件
问题描述 im聊天H5页面,在iOS系统下,inpu获取焦点弹出系统虚拟键盘时,会出现挡住input的情况,十分影响用户体验. bug图 解决方法: html: <input type=&quo ...
- dotnet 如何在 Mock 模拟 Func 判断调用次数
在 dotnet 程序有很好用的 Mock 框架,可以用来模拟各种接口和抽象类,可以用来测试某个注入接口的被调用次数和被调用时传入参数.本文告诉大家如何在 Mock 里面模拟一个 Func 同时模拟返 ...
- 【2016常州一中夏令营Day2】
小 W 学数学[问题描述]为了测试小 W 的数学水平,果果给了小 W N 个点,问他这 N 个点能构成的三角形个数.[输入格式]第一行一个整数 N,代表点数.接下来 N 行,每行两个非负整数 X.Y, ...
- 善用GIMP(Linux下的Photoshop),图像处理轻松又自由
善用GIMP(Linux下的Photoshop),图像处理轻松又自由 作者: 善用佳软 日期: 2013-02-16 分类: 2 图像影音 标签: GIMP, image 1. GIMP是什么? GI ...