hdu 4965 矩阵快速幂 矩阵相乘性质
Fast Matrix Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170 Accepted Submission(s): 99
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.
The end of input is indicated by N = K = 0.
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
56
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 1005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,k;
int a[N][],b[][N],d[][],f[N][],g[N][N],h[N][N];
int ans; typedef struct{
int m[][];
} Matrix; Matrix e,P; Matrix I = {,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
}; Matrix matrixmul(Matrix aa,Matrix bb)
{
int i,j,kk;
Matrix c;
for (i = ; i <= k; i++)
for (j = ; j <= k;j++)
{
c.m[i][j] = ;
for (kk = ; kk <= k; kk++)
c.m[i][j] += (aa.m[i][kk] * bb.m[kk][j])%mod;
c.m[i][j] %= mod;
}
return c;
} Matrix quickpow(int num)
{
Matrix m = P, q = I;
while (num >= )
{
if (num & )
q = matrixmul(q,m);
num = num >> ;
m = matrixmul(m,m);
}
return q;
} int main()
{
int i,j,o;
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n== && k==) break;
memset(d,,sizeof(d));
memset(f,,sizeof(f));
memset(g,,sizeof(g));
memset(h,,sizeof(h));
ans=;
for(i=;i<=n;i++){
for(j=;j<=k;j++){
scanf("%d",&a[i][j]);
}
} for(i=;i<=k;i++){
for(j=;j<=n;j++){
scanf("%d",&b[i][j]);
}
} for(i=;i<=k;i++){
for(o=;o<=k;o++){
for(j=;j<=n;j++){
d[i][o]+=(b[i][j]*a[j][o])%;
}
d[i][o]%=;
P.m[i][o]=d[i][o];
}
} e=quickpow(n*n-); for(i=;i<=n;i++){
for(o=;o<=k;o++){
for(j=;j<=k;j++){
f[i][o]+=(a[i][j]*e.m[j][o])%;
}
f[i][o]%=;
}
} for(i=;i<=n;i++){
for(o=;o<=n;o++){
for(j=;j<=k;j++){
g[i][o]+=(f[i][j]*b[j][o])%;
}
g[i][o]%=;
}
}
/*
for(i=1;i<=n;i++){
for(o=1;o<=n;o++){
for(j=1;j<=n;j++){
h[i][o]+=(g[i][j]*g[j][o])%6;
}
h[i][o]%=6;
}
} */ for(i=;i<=n;i++){
for(o=;o<=n;o++){
ans+=g[i][o];
}
}
printf("%d\n",ans); } return ;
}
hdu 4965 矩阵快速幂 矩阵相乘性质的更多相关文章
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- POJ 3734 Blocks(矩阵快速幂+矩阵递推式)
题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 . 设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...
- 矩阵快速幂/矩阵加速线性数列 By cellur925
讲快速幂的时候就提到矩阵快速幂了啊,知道是个好东西,但是因为当时太蒟(现在依然)没听懂.现在把它补上. 一.矩阵快速幂 首先我们来说说矩阵.在计算机中,矩阵通常都是用二维数组来存的.矩阵加减法比较简单 ...
- POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 27277 Accepted: ...
- 培训补坑(day10:双指针扫描+矩阵快速幂)
这是一个神奇的课题,其实我觉得用一个词来形容这个算法挺合适的:暴力. 是啊,就是循环+暴力.没什么难的... 先来看一道裸题. 那么对于这道题,显然我们的暴力算法就是枚举区间的左右端点,然后通过前缀和 ...
- 快速幂 & 矩阵快速幂
目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分 ...
- 矩阵快速幂模板(pascal)
洛谷P3390 题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格 ...
- 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 ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- 贴一发STL源码
int my_lower_bound(int size, long long key){ int first = 0, middle; int half, len; len = si ...
- 部署Geoserver tomcat部署geoserver
1. 下载Geoserver War 包. 2.把geoserver.war文件放到 webapps文件夹下 3.添加Tomcat 用户 解压文件conf文件夹下 修改tomcat-users.xml ...
- abp viewmodel的写法
我的写法 public class QuotaCreateOrEditViewModel { public QuotaDto LoanQuota { get; set; } public bool I ...
- Mac下搜索神兵利器Alfred 3.1.1最新和谐版
http://bbs.feng.com/read-htm-tid-9891194.html 相比Windows而言Mac自带的Spotlight搜索已经非常强大了,尤其是Mac OS Yosemite ...
- python之文件读写操作(r/r+/rb/w/w+/wb/a/a+/ab)的作用
'r':只读.该文件必须已存在. 'r+':可读可写.该文件必须已存在,写为追加在文件内容末尾. 'rb':表示以二进制方式读取文件.该文件必须已存在. 'w':只写.打开即默认创建一个新文件,如果文 ...
- postman使用--构建工作流和newman
构建工作流 在使用“Collection Runner”的时候,集合中的请求执行顺序就是请求在Collection中的显示排列顺序.但是,有的时候我们不希望请求按照这样的方式去执行,可能是执行完第一个 ...
- 获得Java中System对应一些属性值
public static void main(String[] args){ System.out.println("Java运行时环境版本:\n"+System.getProp ...
- 51nod 1265 四点共面——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1265 以其中某一点向其它三点连向量,若四点共面,这三个向量定义的平行六面体 ...
- bzoj5183 [Baltic2016]Park
题目描述: bz luogu 题解: 把坐标系看反了持续$WA$系列. 对偶图+并查集维护. 先处理出树对树.树对墙的空隙,然后把人和空隙按从小到大排序. 用并查集维护四面墙之间是否能互相隔断. 代码 ...
- SpringBoot 多线程
Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程.使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.而实际开发中任务一般是非阻 ...