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. Leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  2. 【poj1112】 Team Them Up!

    http://poj.org/problem?id=1112 (题目链接) 题意 将n个人分成两组,每个人有认识的人,要求每一组中的人互相认识,并且两组人数之差尽可能的小,求如何分. Solution ...

  3. Hash_bzoj1862: [Zjoi2006]GameZ游戏排名系统

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  5. Jenkins的FTP上传插件Publish Over FTP Plugin设置支持中文路径

    [系统管理]->[系统设置]->[Publish over FTP]->[Control encoding]->输入[GB2312]或者[UTF-8]

  6. ofo走出校园观察:市场定位导致产品错位?

    Ofo和摩拜单车虽然同样都是做单车共享,但实际上两者在最初的市场定位是有明显的差异的,因此提供的产品方案也存在巨大的差异. 市场定位不同,导致产品方案的巨大差异 摩拜单车一开始就定位于开放市场,充分的 ...

  7. Guava 集合框架

    在本系列中我们首先来学习一些Guava的集合框架,也就是这个package:com.google.common.collect 在这个包下面有一些通用的集合接口和一些相关的类.   集合类型: BiM ...

  8. python标准模块(二)

    本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...

  9. JS-Math对象

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>M ...

  10. [EmguCV|WinForm] 使用EmguCV內建直方圖工具繪製直方圖(Histogram)-直方圖(Histogram)系列 (1)

    https://dotblogs.com.tw/v6610688/archive/2013/12/20/emgucv_draw_histogram_histogrambox_histogramview ...