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 ...
随机推荐
- CSS字体中英文名称对照表(转)
在css文件中,我们常看到有些字体名称变成了乱码,这是由于网页开发者将中文字体的名字直接写成了中文,而css文件本身没有声明字符编码方式,查看时就出现了乱码.为了避免这种乱码状况出现,可以将css ...
- ABP框架EF6链接Oracle数据库手动迁移
环境:VS2017 + ABP官方模板(不含Zero) +Oracle 11Gx64DB + Oracle 11Gx32Client(PLSQL工具访问) 一.Abp项目的下载以及运行 1.创建ab ...
- dispatch_after中时间的计算
dispatch_after中用的时间是纳秒,所以需要进行转换:desDelayInSeconds(目标时间,比如2s)* NSEC_PER_SEC double delayInSeconds = 0 ...
- The TTY demystified
http://www.linusakesson.net/programming/tty/index.php The TTY demystified Real teletypes in the 1940 ...
- IOS与安卓的远程调试
本地调试H5页面方案总结 http://www.jianshu.com/p/a43417b28280 Fiddler 手机抓包 http://blog.csdn.net/gld824125233/ar ...
- linux安全组配置
万网的是这样子配置的:
- [译]GLUT教程 - 移动镜头3
Lighthouse3d.com >> GLUT Tutorial >> Input >> Moving the Camera III 上一节的示例中我们用键盘更改 ...
- CentOS: Make Command not Found and linux xinetd 服务不能启动
在centos 安装编译器 yum -y install gcc automake autoconf libtool make linux xinetd 服务不能启动: [root@capaa xin ...
- UIWebView的全屏截图
项目开发中,我们可能会遇到如下的应用场景:将一篇文章,进行截屏(需要全屏截取,包括滚动部分)后,分享到新浪微博.邮箱等等.前段时间,我在应用开发中实现了该功能,代码也是从网上找到的,自己整理了一下.主 ...
- 将普通用户添加至sudoers列表
编辑/etc/sudoers文件,在尾部添加如下内容: myusername ALL=(ALL) ALL myusername ALL=(ALL) NOPASSWD: ALL 其中需要将红色部分替换成 ...