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 ...
随机推荐
- 【codeforces 789A】Anastasia and pebbles
[题目链接]:http://codeforces.com/contest/789/problem/A [题意] 有n种物品,每种物品有wi个; 你有两个口袋,每个口袋最多装k个物品; 且口袋里面只能装 ...
- React MVC框架 <某某后台商品管理开源项目> 完成项目总结
**百货后台商品信息开源项目 1.利用React app脚手架 2.封装打包 buid 3.更偏向于后台程序员开发思维 4.利用的 react -redux react-router-dom ...
- 2019-11-20-Github-给仓库上传-NuGet-库
title author date CreateTime categories Github 给仓库上传 NuGet 库 lindexi 2019-11-20 08:18:14 +0800 2019- ...
- ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(3)
接下来完成用户.角色的增删查改,以及用户角色.权限的设置 对用户表.角色表做了一些扩展如下[可以更加自己需要增减字段] 相应的M_UserProfile.cs.M_Roles.cs进行扩展 using ...
- Loj3033 JOISC 2019 Day2两个天线
Loj3033 JOISC 2019 Day2两个天线 下午唯一听懂的题目但,但还是比较模糊.写一篇题解来加深一下印象. 题目大意:给定\(n\)根天线,第\(i\)跟天线的高度为\(h_i\),切它 ...
- 2019-1-29-UWP-IRandomAccessStream-与-Stream-互转
title author date CreateTime categories UWP IRandomAccessStream 与 Stream 互转 lindexi 2019-01-29 16:33 ...
- ASP.NET MVC API与JS进行POST请求时传递参数 -CHPowerljp原创
在API前添加 [HttpPost] 表示只允许POST方式请求 [HttpPost] public IHttpActionResult Get_BIGDATA([FromBody]Datas ...
- Spring Boot + Docker + K8S 简单示例
前言 最近看了看k8s,感觉用这个管理docker确实比自己写一坨脚本进步太多了,简直不是一个次原的东西. 看着k8s的官方文档随手写了个小Demo,一个基于k8s的spring boot服务. 代码 ...
- Java 中级 学习笔记 1 JVM的理解以及新生代GC处理流程
写在最前 从毕业到现在已经过去了差不多一年的时间,工作还算顺利,但总是离不开CRUD ,我觉得这样下去肯定是不行的,温水煮青蛙,势必有一天,会昏昏沉沉的迷失在温水里.所以,需要将之前学习JAVA 当中 ...
- SpringBoot-2.1.1系列二:使用websocket
1.什么是websocket? WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信--允许服务器主动发送信息给客户端. 2.为什么需要使用 ...