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. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  2. Jquery打叉怎么办

    选中报错文件右键MyEclipse>Exclude From xxxx

  3. BZOJ1010 [HNOI2008]玩具装箱toy

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  4. Scala: 包对象

    包对象最重要的用途是兼容旧的类库,或者为某些数据类型提供增强版本:一般我们可以将其作为扩展工具方法或数据来使用

  5. bash的变量设置

    1. myname=zhangsan //设置变量 2. echo $myname //显示变量 或者:echo ${myname} 3. myname="my name is $mynam ...

  6. GridView控件RowDataBound事件中获取列字段值的几种途径 !!!

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == ...

  7. PhyLab2.0需求与功能分析改进文档(NABCD)

    PhyLab1.0需求规格说明文档 1. 概述 1.1 项目概述 软剑攻城队小组于2015学年开发了PhyLab物理实验网站,一经发布好评如潮.网站的核心功能是提供预习报告和自动数据处理,而后加入了论 ...

  8. 看了汤姆大叔的“你真懂JavaScript吗?”的一些感慨

    看了汤姆大叔的“你真懂JavaScript吗?”,里面有5道题目,我都一一作了,然后在chrome的控制台里面运行了一遍,虽然只错了一道,但还是细细读了下答案,在此总结一下,看看是否对大家对这些Jav ...

  9. .net 运用YUI相关的dll压缩js (按照自己的规则,想想都觉得强大和有趣)

    写在前面 不管是做前端的还是做后台的,不管是懂javaScript的还是不太懂JavaScript的人,我想都或多或想的知道些许js压缩对于页面性能提升的效应吧. 之前老喜欢用在线压缩工具去压缩js, ...

  10. 使用LaTeX编辑数学公式

    首先在博客园的页首html里添加以下代码: <script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex ...