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 ...
随机推荐
- jQuery异步框架探究2:jQuery.Deferred方法
(本文针对jQuery1.6.1版本号)关于Deferred函数的描写叙述中有一个词是fledged,意为"羽翼丰满的",说明jQuery.Deferred函数应用应该更成熟. 这 ...
- HTML to PDF pechkin
1. Goto Nuget 下载 Pechkin 控件 2. 创建需要打印的的PDF controller 和 Action, 这里会调用其他页面的内容进行打印. public ActionResul ...
- Manifest.xml中删除了『存储/修改删除SD卡中的内容』和『手机通话/读取手机状态和身份』权限,但生成apk安装软件时仍提示 允许应用程序了解或使用这两个权限
原因:Android系统会给targetSdk版本为“4”以下的应用自动分配WRITE_EXTERNAL_STORAGE 和 READ_PHONE_STATE 权限. 解放办法:在manifest.x ...
- 类中的internal成员可能是一种坏味道
前言 最近除了搞ASP.NET MVC之外,我也在思考一些编程实践方面的问题.昨天在回家路上,我忽然对一个问题产生了较为清晰的认识.或者说,原先只是有一丝细微的感觉,而现在将它和一些其他的方面进行了联 ...
- 用unity3d实现简单的主server连接
用unity3d实现简单的主server连接 參考自鹰大的网络实例 -------------------------------------------------华丽的切割线----------- ...
- 【.net项目中。。】.net一般处理程序使用方法
1.基本原理图 IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHa ...
- Android开发系列之系统源码目录
相信大家对于Google给出的那副经典Android架构图非常的熟悉,从下往上依次是Linux内核层(主要是负责硬件管理调度),HAL层(主要是硬件抽象层),libs层+Runtime,Framewo ...
- 使用新版MonoDevelop来进行unity工程调试
现在可以使用新版的MonoDeveloper来调试Unity工程了,新版的MonoDeveloper界面上漂亮很多,而且使用.调试上感觉也更舒服了.先上一张图: 安装方法如下: 登陆网址:http ...
- JFinal中Controller的应用
部分方法: 1.获取参数:getPara(String name); getParaToInt(String name) ,将返回参数的值转为int: getPara() ,url中参数连接为/v1- ...
- Cassandra安装和初次使用
Cassandra安装和初次使用 卡珊德拉(Cassandra)又译卡桑德拉.卡珊卓,为希腊.罗马神话中特洛伊(Troy)的公主,阿波罗(Apollo)的祭司.因神蛇以舌为她洗耳或阿波罗的赐予而有预言 ...