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. org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。

    二月 25, 2016 9:24:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRul ...

  2. 百度地图学习(Ⅰ)-Android端地图的显示及简单应用

    ps:(1.地图应用一定要在真机测试: 2.Design By:Android Stdio: 3.百度地图官方参考链接(http://developer.baidu.com/map/index.php ...

  3. Codeforces Round #389 Div.2 A. Santa Claus and a Place in a Class

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Scala Trait

    Scala Trait 大多数的时候,Scala中的trait有点类似于Java中的interface.正如同java中的class可以implement多个interface,scala中的cals ...

  5. CF 701B Cells Not Under Attack(想法题)

    题目链接: 传送门 Cells Not Under Attack time limit per test:2 second     memory limit per test:256 megabyte ...

  6. C# 开源项目一

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

  7. JavaScript事件类型

    JavaScript事件类型 Web浏览器中可能发生的事件有很多类型.这里我将主要将下面几种常用的事件类型: UI事件 焦点事件 鼠标与滚轮事件 键盘与文本事件 复合事件 变动事件 HTML5事件 设 ...

  8. tomcat添加https

    1.下载依赖包    wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz      wget http://archive.apache. ...

  9. WinForm------如何将GridControl数据导出到Excel

    转载: http://www.cnblogs.com/xiaofengfeng/archive/2011/11/22/2258906.html 代码: SaveFileDialog saveFileD ...

  10. 阻塞式socket例子学习

    /************************************************************************* > File Name: Win_Serve ...