http://acm.hdu.edu.cn/showproblem.php?pid=4965

1006

Fast Matrix Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 238    Accepted Submission(s): 128

Problem Description
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.

 
Input
The 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.

 
Output
For 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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  4970 4969 4968 4967 4966

题意:给出n*k的矩阵A和k*n的B,求(AB)^(n*n)结果矩阵中各元素模6 之和。(n<=1000,k<=6)

题解:(A*B)^(n*n)=A * (B*A)^(n*n-1) * B,(B*A)是k*k的矩阵,k最大只有6,简直碉炸,矩阵快速幂就行了。

之前的多校训练也有一题hdu4920,是模3矩阵乘法:http://www.cnblogs.com/yuiffy/p/3893018.html

在那题中我已经研究了各种矩阵乘法的优化,例如要kij循环而不是ijk循环,对一个小数取模的话会有很多0,可以在第二重循环中if(a[i][k]==0)就跳出,而且由于取模后数字很少,可以直接用一个三维数组l[i][j][k]来事先运算好 (i+j*k)%MOD,这样我们就又不用乘法又不用取模,简直极速。

但是这题如果直接(A*B)^(n*n)的话,就算已经极速优化了还是不行,我都怕。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back int A[][];
int B[][];
int C[][];
int D[][];
int n,K; int liu[][][]; void check(int A[][],int n){
int i,j;
for(i=;i<n;i++){
for(j=;j<n;j++)
printf("%2d",A[i][j]);
puts("");
}
} int F[][]; void chen2(int C[][],const int A[][],const int B[][],const int n,const int m,const int K) {
int i,j,k;
for(i=;i<n;i++)
for(j=;j<m;j++)
F[i][j]=;
//cout<<n<<','<<m<<','<<K<<endl;
for(k=; k<K; k++)
for(i=; i<n; i++){
if(A[i][k]==)continue;
for(j=; j<m; j++) {
//F[i][j]+=A[i][k]*B[k][j];
F[i][j]=liu[ F[i][j] ][ A[i][k] ][ B[k][j] ];
//printf("F[%d][%d]+=A[%d][%d]*B[%d][%d]=%d*%d %d\n",i,j,i,k,k,j,A[i][k],B[k][j],F[i][j]);
}
}
for(i=;i<n;i++)
for(j=;j<m;j++)
C[i][j]=F[i][j];
} void powmod(int C[][],int x,int K,int D[][]) {
int i,j,k;
mz(D);
for(i=;i<K;i++)
D[i][i]=;
while(x) {
if(x&)chen2(D,D,C,K,K,K);
// puts("D:");
// check(D,K);
// puts("C:");
// check(C,K);
// printf("x=%d=%xH\n",x,x);
x>>=;
chen2(C,C,C,K,K,K);
}
} int biu[]; void init(){
int i,j,k;
for(i=;i<;i++)
for(j=;j<;j++)
for(k=;k<;k++)
liu[i][j][k]=(i+j*k)%;
for(i=;i<;i++)
biu[''+i]=i;
} char ch;
inline void read(int &x){
while(!((((ch = getchar()) >= '') && (ch <= ''))));
x=biu[ch];
} int main() {
int i,j;
init();
while(scanf("%d%d",&n,&K)!=EOF) {
mz(A);mz(B);mz(C);mz(D);
if(n== && K==)break;
for(i=; i<n; i++)
for(j=; j<K; j++)
read(A[i][j]);
for(i=; i<K; i++)
for(j=; j<n; j++)
read(B[i][j]);
chen2(C,B,A,K,n,n);
//chen2(C,A,B,n,n,K);
//check(C,n);
powmod(C,n*n-,K,D);
//powmod(C,n*n,n,D);
//check(D,K);
chen2(D,A,D,n,K,K);
chen2(D,D,B,n,n,K);
//check(D,n);
int ans=;
for(i=;i<n;i++)
for(j=;j<n;j++)
ans+=D[i][j];
printf("%d\n",ans);
}
return ;
}

hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律的更多相关文章

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

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

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

  3. Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  4. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

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

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

  6. ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

    Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...

  7. bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希

    题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...

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

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

  9. HDU_4965 Fast Matrix Calculation 2014多校9 矩阵快速幂+机智的矩阵结合律

    一开始看这个题目以为是个裸的矩阵快速幂的题目, 后来发现会超时,超就超在  M = C^(N*N). 这个操作,而C本身是个N*N的矩阵,N最大为1000. 但是这里有个巧妙的地方就是 C的来源其实 ...

随机推荐

  1. TYVJ1427 小白逛公园

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述     小新经常陪小白去公园玩,也就是所谓的遛狗啦…在小新家附近有一条“公园路”,路的一边从南到北依次排着n个 ...

  2. OrCAD Capture使用记录

    1.安装 ①修改host_ID为主机名称(如果填不对,安装过程会提示你再填一次):其后跟的是机子的MAC地址,用ipconfig /all 可以查看. ②下一行的FLEMlm,后面跟的是文件cdslm ...

  3. JavaScript制作时钟特效

    需求说明:制作显示年.月.日.星期几并且显示上午(AM)和下午(PM)的 12进制的时钟,具体效果如下所示: 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C// ...

  4. K米--案例分析

    第三次作业- -K米软件评测 第一部分 调研.评测 评测: 下载并使用.描述最简单直观的个人第一次上手体验: 第一次下载打开.这个简介粗矿的界面让偶着实吓了一跳.界面设计的有板有眼.直接了当.就像是在 ...

  5. 北京地铁站点遍历最少经站次数问题普遍意义上是一个NP问题,目前不存在多项式时间算法能够解决该问题

    http://www.cnblogs.com/jiel/p/5852591.html 众所周知求一个图的哈密顿回路是一个NPC问题: In the mathematical field of grap ...

  6. 面向服务架构(SOA)和企业服务总线(ESB)

    http://www.cnblogs.com/shanyou/archive/2008/04/19/1161452.html 学习和研究在企业中实施面向服务架构(SOA),简单回顾SOA和ESB,重点 ...

  7. php 简单分页类

    /**  file: page.class.php   完美分页类 Page  */ class Page {  private $total;          //数据表中总记录数  privat ...

  8. js006-面向对象的程序设计

    js006-面向对象的程序设计 面向对象(Object-Oriented,OO)的语言有一个标志,那就是他们都有类的概念.而通过类可以创建多个具有相同属性和方法的对象. ECMA-262把对象定义为: ...

  9. 《Struts2.x权威指南》学习笔记1

    第2章 Struts的hello world 在介绍hello world项目前,文中要求下载和安装Struts2,主要是下载lib库和文档,可用于通过命令行进行代码编译.由于公司采用IntelliJ ...

  10. You Can Do Research Too

    You Can Do Research Too I was recently discussing gatekeeping and the process of getting started in ...