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 ...
随机推荐
- 特别困的学生 UVa12108(模拟题)
一.题目 课堂上有n个学生(n<=10).每个学生都有一个“睡眠-清醒”周期,其中第i个学生醒Ai分钟后睡Bi分钟,然后重复(1<=Ai,Bi<=5),初始第i个同学处于他的周期的C ...
- 如何在vue项目中使用sass(scss)
1.用npm/cnpm/yarn安装sass的依赖包 npm install --save-dev sass-loader npm install --save-dev node-sass 或者: y ...
- mysql安装(docker)
mkdir /opt/mysql vim /opt/mysql/Dockerfile 5.7 FROM alpine FROM mysql:5.7.26 EXPOSE 3306 8.0 FROM al ...
- Python 输入输出 数据类型 变量
python输入输出 数据类型 变量 输入输出 print()在Python3中是函数 >>>print('hello world') #print注意print前面不要有任何空格 ...
- Lucene入门基础教程
http://www.linuxidc.com/Linux/2014-06/102856.htm
- javase(12)_集合框架_Queue
一.Queue Queye接口体系图 体系分析: Deque实现类:ArrayDeque, LinkedList(数组和链表实现双向队列) BlockingDeque实现类:LinkedBlockin ...
- GIMP素描效果
1/打开图片,拖动图片到GIMP软件 2/复制两次图层 3/选中最上面的一个图层,mode改为Dodge 4/点击Color,选择Invert,可以看到图片变淡了 5/点击Filters,Distor ...
- Kafka创建&查看topic,生产&消费指定topic消息
启动zookeeper和Kafka之后,进入kafka目录(安装/启动kafka参考前面一章:https://www.cnblogs.com/cici20166/p/9425613.html) 1.创 ...
- 【转】Hive over HBase和Hive over HDFS性能比较分析
转载:http://lxw1234.com/archives/2015/04/101.htm 环境配置: hadoop-2.0.0-cdh4.3.0 (4 nodes, 24G mem/node) h ...
- django第9天(多表操作)
django第9天 models类 class Book(Model): id = AutoField(primary_key=True) name = CharField(max_length=20 ...