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.

矩阵快速幂

 #include<stdio.h>
#include<string.h>
#include<math.h>
typedef long long ll;
const int mod=; struct mat{
int r,c;
int m[][]; //经测试最大开成590*590的 ll 型矩阵
mat(){}
mat(int r,int c):r(r),c(c){}
void clear(){
memset(m,,sizeof(m));
} mat operator+(mat a)const{
mat ans(r,c);
for(int i=;i<=r;i++){
for(int j=;j<=c;j++){
ans.m[i][j]=(m[i][j]+a.m[i][j])%mod;
}
}
return ans;
} mat operator*(mat a)const{
mat tmp(r,a.c);
int i,j,k;
for(i=;i<=tmp.r;i++){
for(j=;j<=tmp.c;j++){
tmp.m[i][j]=;
for(k=;k<=c;k++){
tmp.m[i][j]=(tmp.m[i][j]+(m[i][k]*a.m[k][j])%mod)%mod;
}
}
}
return tmp;
} mat operator^(int n)const{ //需要时可以用 ll n,注意运算符优先级比较低,多用括号;
mat ans(r,r),tmp(r,r);
memcpy(tmp.m,m,sizeof(tmp.m));
ans.clear();
for(int i=;i<=ans.r;i++){
ans.m[i][i]=;
}
while(n){
if(n&)ans=ans*tmp;
n>>=;
tmp=tmp*tmp;
}
return ans;
} void print()const{
for(int i=;i<=r;i++){
for(int j=;j<=c;j++){
printf("%d",m[i][j]);
if(j==c)printf("\n");
else printf(" ");
}
}
} }; int m1[][],m2[][],tmp[][],tmp2[][],tmp3[][]; int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF&&n+k){
int i,j,p;
for(i=;i<=n;i++){
for(j=;j<=k;j++)scanf("%d",&m1[i][j]);
}
for(i=;i<=k;i++){
for(j=;j<=n;j++)scanf("%d",&m2[i][j]);
}
for(i=;i<=k;i++){
for(j=;j<=k;j++){
tmp[i][j]=;
for(p=;p<=n;p++){
tmp[i][j]+=m2[i][p]*m1[p][j];
}
tmp[i][j]%=;
}
}
mat a(k,k);
memcpy(a.m,tmp,sizeof(tmp));
a=(a^(n*n-));
memcpy(tmp,a.m,sizeof(tmp));
for(i=;i<=n;i++){
for(j=;j<=k;j++){
tmp2[i][j]=;
for(p=;p<=k;p++){
tmp2[i][j]+=m1[i][p]*tmp[p][j];
}
tmp2[i][j]%=;
}
}
for(i=;i<=n;i++){
for(j=;j<=n;j++){
tmp3[i][j]=;
for(p=;p<=k;p++){
tmp3[i][j]+=tmp2[i][p]*m2[p][j];
}
tmp3[i][j]%=;
}
}
int ans=;
for(i=;i<=n;i++){
for(j=;j<=n;j++)ans+=tmp3[i][j];
}
printf("%d\n",ans);
}
return ;
}

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

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

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

  2. Fast Matrix Calculation 矩阵快速幂

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

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

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

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

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

  5. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

  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 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...

随机推荐

  1. Weka中数据挖掘与机器学习系列之数据格式ARFF和CSV文件格式之间的转换(五)

    不多说,直接上干货! Weka介绍: Weka是一个用Java编写的数据挖掘工具,能够运行在各种平台上.它不仅提供了可以直接用于数据挖掘的软件,还提供了src代码,使用者可以修改源代码,进行二次开发. ...

  2. daay04流程控制之for循环

    for循环主要用于循环取值 student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb'] # i=0 # while i < len(student ...

  3. windows消息传送(自定义消息和WM_COPYDATA)

    通过SendMessge实现的进程间通信. 0x01 自定义消息 1,WINDOWS中自定义消息的定义和使用: (1)在WNDOWS中消息分系统消息和自定义消息.系统消息定义从0到0x3FF,使用0x ...

  4. 安装连接mysql8时候遇到的问题以及解决(转)

    官网下载mysql8的安装包: https://dev.mysql.com/downloads/ 下一步安装即可. mysql8增加了传说中的安全性校验 遇到的几个问题: 1.natcat连接不上.参 ...

  5. 如何正确认识Docker Kubernetes 和 Apache Mesos

    参考链接: http://geek.csdn.net/news/detail/229382

  6. 初识linux------用户和用户组

    事先说明 本Linux的版本为Ubuntu. 为避免一些初学者由于权限问题特此事先说明,在非root权限下时,所有的代码加sudo:如下 (1)不在root权限 sudo useradd -m 用户名 ...

  7. 区分IE版本的js代码

    function IEVersion() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isIE = userAgen ...

  8. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  9. python的time模块

    #encoding=utf-8 import time # 返回时间戳 print time.time() # 延迟运行单位为s,如下为延迟3s time.sleep(3) # 转换时间戳为时间元组( ...

  10. HDU1754-I Hate It (线段树)

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others ...