hdu4965 Fast Matrix Calculation 矩阵快速幂
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 矩阵快速幂的更多相关文章
- HDU 4965 Fast Matrix Calculation 矩阵快速幂
题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...
- Fast Matrix Calculation 矩阵快速幂
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
- hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...
- bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希
题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...
- HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- 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 ...
随机推荐
- LeetCode刷题 Flood Fill 洪水填充问题
An image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...
- GFS中文翻译
Google文件系统 GFS是一个可扩展的分布式文件系统,用于大型的.分布式的.对大量数据进行访问的应用.它运行于廉价的普通硬件上,但可以提供容错功能.它可以给大量的用户提供总体性能较高的服务. 1. ...
- Traumland--梦乡--IPA--德语
德国电影<<英俊少年>>的插曲.
- ngnix笔记
ngnix可通过-s 参数控制,如quit正常退出:reload重载配置文件,具体参考:http://nginx.org/en/docs/switches.html ngnix的指令解释请参考这里:h ...
- ubuntu多显示器单触摸屏校准
多显示器单触摸屏屏幕校准 0.触摸屏重定向 sudo xinput map-to-output 13 DP1 #将触摸屏映射到指定的显示器 其中:13为触摸屏设备id,可通过 xinput命令查看 ...
- ubuntu gnome桌面隐藏顶栏
注意:ubuntu 14.04.5默认的为unity桌面,有多点触发,没有自带Tweak Tool工具.需安装gnome 桌面,可参见我的另一随笔. 环境: ubuntu 14.04.5 gnome ...
- 自动化创建tornado项目
tornado目录结构: index.py 入口文件 app app目录 |___ __init__.py 初始化脚本 |___ templates 模板目录 | |___ index ...
- angular2的模板语法
Angular 应用管理着用户之所见和所为,并通过 Component 类的实例(组件)和面向用户的模板来与用户交互. 从使用模型-视图-控制器 (MVC) 或模型-视图-视图模型 (MVVM) 的经 ...
- 解决jsp表达式不能解析的问题
在jsp页面用了表达式,但是出现了表达式不能解析的问题 出现的页面如下 原因:web.xml的版本过低,maven自动生成的web.xml版本为2.3,只有2.3以上的版本才支持表达式 解决方法:改w ...
- linux 基本命令大全
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...