题目链接

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;
}
}

最新文章

  1. sqlyog导出json数据格式支持mysql数据转存mongodb
  2. Jquery 插件\Js 插件收集
  3. 我的Objective-C系列文章
  4. mySql中IFNULL的使用说明
  5. ARM学习篇 SDRAM理解
  6. 使用reids结合wcf实现集群模式下的聊天室功能
  7. 【转】Mybatis/Ibatis,数据库操作的返回值
  8. centos中安装chromium和flash
  9. Start of Something New
  10. 蚁群算法 matlab程序(已执行)
  11. Swift - UIPasteboard剪贴板的使用详解(复制、粘贴文字和图片)
  12. POJ3259负环判定
  13. 我的IT开源之路
  14. for语句,你真正搞懂了吗?
  15. 基于BCGP库的界面效果
  16. Linux下硬盘分区
  17. Django 2.0.4 微博第三方登录
  18. IdentityServer4【QuickStart】之切换到混合流并且添加API访问
  19. poj 1151 (未完成) 扫描线 线段树 离散化
  20. hdu 2688

热门文章

  1. Window 7 开 WIFI做热点
  2. R语言初识
  3. WordPress系列之钩子hook的作用及基本用法
  4. android端StarIO热敏打印机打印小票
  5. Junit内部解密之三: 单元测试用例运行的全过程
  6. 《TomCat与Java Web开发技术详解》(第二版) 第三章节的学习总结--利用Context元素来自定义web应用的存储位置
  7. 【cogs182】【USACO Jan07】均衡队形【st表】
  8. ubuntu 16.04中卸载软件。
  9. erlang的非平衡的二叉树的操作
  10. Linux进程间通信(四) - 共享内存