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. H3C 常用的IPv6地址类型及格式

  2. java的四种代码块

    用{}括起来的称为代码块: 普通代码块:类中方法的方法体 构造代码块:类中{}直接括起来的语句,每次创建对象都会被调用,先于构造函数执行 静态代码块:类中static{}括起来的语句,只执行一次,先于 ...

  3. Linux 内核VLB 总线

    另一个对 ISA 的扩展是 VESA Local Bus(VLB) 接口总线, 它扩展了 ISA 连接器, 通过 添加第 3 个知道长度的槽位. 一个设备可只插入这个额外的连接器(不用插入 2 个关联 ...

  4. Python12_关于文件概念的讨论与序列化

    文件是什么? 存储在一些设备上的信息的集合.一堆字节: ====================================================到底什么是二进制文件.和文本文件,它们有 ...

  5. java_字段初始化的规律、静态方法中访问类的实例成员、查询创建对象的个数

    字段初始化规律: 当执行如下代码时 class InitializeBlockClass{ public int field=100; { field=200; } public Initialize ...

  6. HashMap、lru、散列表

    HashMap HashMap的数据结构:HashMap实际上是一个数组和链表("链表散列")的数据结构.底层就是一个数组结构,数组中的每一项又是一个链表. hashCode是一个 ...

  7. JAVA CONCURRENT FRAMEWORK

    1.ConcurrentHashMap 在并发中使用hashmap容易导致死锁,hashmap存在以下问题 1.线程不安全的hashmap 在多线程环境下使用hashmapPut操作时会引起死循环,因 ...

  8. C# ref参数

    ref关键字用于将方法内的变量改变后带出方法外.具体我们通过例子来说明: static void Main(string[] args) { int c = 0; Add(1, 2,ref c); C ...

  9. docker容器内存占用过高(例如mysql)

    简介 该文章适用于配置低,特别是内存低的服务器,在用容器部署服务时有可能会因为容器占用内存过高导致服务挂掉时参考解决(不是运行在容器里的话,也是可以修改mysql的配置文件限制内存占用) 最近用doc ...

  10. $loj526\ [LibreOJ\ \beta\ Round\ \#4]$ 子集 图论

    正解:图论 解题报告: 传送门$QwQ$ 发现最大团不好求,于是考虑求最大独立集.也就把所有$gcd(i,j)\cdot gcd(i+1,j+1)=1$的点之间连边,然后求最大独立集. 发现依然不可做 ...