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 * B) ^ (n * n)
然后将M中所有的元素对6取余后求和
思路
矩阵结合律。。
M = (A * B) * (A * B) * (A * B) * (A * B) * (A * B) * (A * B) * (A * B) * (A * B) ……
其实也等价于
M = A * (B * A) * (B * A) * (B * A) * (B * A) * (B * A) * (B * A) * B
因为 n 比较大 k 比较小
如果用 (A * B) 做矩阵快速幂的话,会T
所有 用 B * A 做矩阵快速幂 最后左乘A 再右乘B 就可以了
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double EI = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 10;
const int MOD = 6;
struct Matrix
{
int a[10][10];
int n;
Matrix() {}
Matrix operator * (Matrix const &b)const
{
Matrix res;
res.n = n;
CLR(res.a, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
res.a[i][j] = (res.a[i][j] + this->a[i][k] * b.a[k][j]) % MOD;
return res;
}
};
Matrix pow_mod(Matrix base, int a, int n)
{
Matrix ans;
CLR(ans.a, 0);
for (int i = 0; i < n; i++)
ans.a[i][i] = 1;
ans.n = n;
while (a > 0)
{
if (a & 1)
ans = ans * base;
base = base * base;
a >>= 1;
}
return ans;
}
int A[maxn][maxn], B[maxn][maxn], ans[maxn][maxn];
int main()
{
int n, k;
while (scanf("%d%d", &n, &k) && (n || k))
{
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
scanf("%d", &A[i][j]);
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++)
scanf("%d", &B[i][j]);
Matrix base;
base.n = k;
CLR(base.a, 0);
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++)
for (int l = 0; l < n; l++)
base.a[i][j] = (base.a[i][j] + B[i][l] * A[l][j]) % MOD;
base = pow_mod(base, n * n - 1, k);
CLR(ans, 0);
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++)
for (int l = 0; l < k; l++)
ans[i][j] = (ans[i][j] + base.a[i][l] * B[l][j]) % MOD;
CLR(B, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int l = 0; l < k; l++)
B[i][j] = (B[i][j] + A[i][l] * ans[l][j]) % MOD;
int tot = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
tot += B[i][j];
cout << tot << endl;
}
}
HDU - 4965 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\) ...
- 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 矩阵快速幂
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...
- Fast Matrix Calculation 矩阵快速幂
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...
- HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- hdu 4965 Fast Matrix Calculation
题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- 【Python数据分析】
索引对象的其他功能 ①更换索引 ②对齐 ③删除 一.更换索引 我们已经知道,数据结构一旦声明,index对象就不能改变 事实上,我们重新定义索引之后,我们就能够用现有的数据结构生成一个新的数据机构 p ...
- 【VBA】获取当前工作表的用户名
如何使用VBA获取当前工作表的用户名呢?请看如下代码: Sub 获取当前工作表的用户名() MsgBox "当前工作表的用户名为:" & Application.UserN ...
- AOP切面编程在android上的应用
代码地址如下:http://www.demodashi.com/demo/12563.html 前言 切面编程一直是一个热点的话题,这篇文章讲讲一个第三方aop库在android上的应用.第三方AOP ...
- Android 报错记录
IOException java.io.IOException: Permission denied: 权限不足.原因可能是: 未加入权限 <uses-permission android ...
- Android:使用ZXing生成二维码(支持加入Logo图案)
ZXing是谷歌的一个开源库.能够用来生成二维码.扫描二维码.本文所介绍的是第一部分. 首先上效果图: ZXing相关各种文件官方下载地址:https://github.com/zxing/zxing ...
- 借助Anyproxy实时监控接口调用次数和流量
监控接口调用次数,是为了测试客户端可能会异常频繁的调用服务端接口,出现性能问题. AnyProxy是一个开放式的HTTP代理服务器. github: https://github.com/alibab ...
- iOS Masonry 抗压缩 抗拉伸
约束优先级: 在Autolayout中每个约束都有一个优先级, 优先级的范围是1 ~ 1000.创建一个约束,默认的优先级是最高的1000 Content Hugging Priority: 该优先级 ...
- leetCode(37):Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 第四篇: Ansible 常用模块使用
一. command | shell 模块 该模块为:执行命令模块 (由于命令模块较为强大,可根据自身对操作系统的熟练程度而增加.所以这里不再介绍类似可以实现同功能模块,比如service模块,需要重 ...
- GitHub 寻宝指南
GitHub 寻宝指南 寻找 Demo 技术栈的关键字搜索,并按更新时间进行排序 生命有限 ,如若是每次我们尝试一个新的技术,总得自己编写一个个 Demo.编写多个 Demo,都得花去个半天八小时的时 ...