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. K-means之matlab实现

    引入 作为练手,不妨用matlab实现K-means 要解决的问题:n个D维数据进行聚类(无监督),找到合适的簇心. 这里仅考虑最简单的情况,数据维度D=2,预先知道簇心数目K(K=4) 理论步骤 关 ...

  2. kali2016.2源

    最近中科大的源出了问题,官方源又会重定向到意大利. 一下源目前亲测可用:kali2016.2源 清华大学 deb http://mirrors.tuna.tsinghua.edu.cn/kali ka ...

  3. Jenkins 2.x新建节点配置(Windows)

    2.0版本以上默认加入了权限插件,所以在进入主界面时是需要登录的. 一.主界面->[系统管理]->[管理节点]->[新建节点],进行节点的添加: 二.输入节点名称,已经选择[Perm ...

  4. crawler:简要了解一下PhantomJS

    有时,我们需要浏览器处理网页,但并不需要浏览,比如生成网页的截图.抓取网页数据等操作.PhantomJS的功能,就是提供一个浏览器环境的命令行接口,你可以把它看作一个“虚拟浏览器”,除了不能浏览,其他 ...

  5. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  6. 重写Object类中的equals方法

    Object是所有类的父亲,这个类有很多方法,我们都可以直接调用,但有些方法并不适合,例如下面的student类 public class Student { //姓名.学号.年纪 private S ...

  7. JPA mysql wildfly jboss 存储时乱码

    首先确保mysql的库,表创建时指定的字符集collation. 可以直接用命令行插入中文,看查询出来是不是中文. insert into live_main_sync (cn_name, creat ...

  8. Struts学习总结-04 上传文件

    1. upload.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...

  9. JavaScriptCore框架介绍

    http://www.cocoachina.com/ios/20140409/8127.html 这个框架其实只是基于webkit中以C/C++实现的JavaScriptCore的一个包装,在旧版本i ...

  10. openssl生成https证书 (转)

    1.首先要生成服务器端的私钥(key文件):openssl genrsa -des3 -out server.key 1024运行时会提示输入密码,此密码用于加密key文件去除key文件口令的命令:o ...