One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.

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 题意 :按照题目所给的要求,求最终答案
思路 : 在一个结构体中去存一个二维矩阵时,最多可以开到 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的更多相关文章

  1. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  2. hdu 4965 Fast Matrix Calculation(矩阵高速幂)

    题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...

  3. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  4. 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 ...

  5. HDU 4965 矩阵快速幂

    顺手写了下矩阵类模板 利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B #include <cstdio> #include <cstring> ...

  6. 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 ...

  7. HDU 4965 Fast Matrix Calculation 矩阵快速幂

    题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...

  8. hdu 4965 矩阵快速幂 矩阵相乘性质

    Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Jav ...

  9. HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律

    一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  10. Hdu 4965(矩阵快速幂)

    题目链接 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

随机推荐

  1. H5 移动端获取当前位置

    3种方法:1.H5自带的方法,获取经纬度2.通过地图提供的JS.获取位置3.通过微信的API(这个需要公众号 / 小程序) 1.通过H5自带的获取经纬度的方法 优点: 需要引用的资源较少,H5自带的方 ...

  2. git checkout简介

    原文: http://web.mit.edu/~thefred/MacData/afs/sipb/project/git/git-doc/git-checkout.html  git checkout ...

  3. linux 使用 /proc 文件系统

    /proc 文件系统是一个特殊的软件创建的文件系统, 内核用来输出消息到外界. /proc 下 的每个文件都绑到一个内核函数上, 当文件被读的时候即时产生文件内容. 我们已经见到 一些这样的文件起作用 ...

  4. linux 从用户空间的 I/O 存取

    刚刚描述的这些函数主要打算被设备驱动使用, 但它们也可从用户空间使用, 至少在 PC- 类 的计算机. GNU C 库在 <sys/io.h> 中定义它们. 下列条件应当应用来对于 inb ...

  5. WindowsDOS命令添加/创建/修改/删除服务

    添加服务 sc <server> create [service name] [binPath= ] <option1> <option2>... 在注册表和服务数 ...

  6. 网摘-获取屏幕dc并且将其画面显示在窗体中

    获取屏幕dc并且将其画面显示在窗体中 HWND hWnd = ::GetDesktopWindow();//获得屏幕的HWND. HDC hScreenDC = ::GetDC(hWnd);   // ...

  7. 关于react打包之后静态资源加载错误的问题

    之前在打包react项目时发现一些问题,打包出来后我的一部分png图标加载不出来,开发者模式发现他们的路径中莫名其妙混入了我在react-router路由中使用<Browserrouter> ...

  8. js 快速取整

    我们要将23.8转化成整数  有哪些方法呢 比如 Math.floor( ) 对数进行向下取整  它返回的是小于或等于函数参数,并且与之最接近的整数 Math.floor(5.1) 返回值 //5 M ...

  9. How to fix nuget Unrecognized license type MIT when pack

    When I packaging license within the nupkg, I will using License to replace licentUrl. I using this c ...

  10. CDM命令实现MySql数据库文件的导出导入

    1.首先进入MySQL的安装目录,找到Bin文件夹,我这里安装的目录是C:\Program Files\MySQL\MySQL Server 8.0\bin ,进入该文件夹后在空白处按下Shift键+ ...