http://poj.org/problem?id=3735

给定一串操作,要这个操作连续执行m次后,最后剩下的值。

记矩阵T为一次操作后的值,那么T^m就是执行m次的值了。(其实这个还不太理解,但是数据一相乘,就是ans)

构造一个0--n的单位矩阵,用第0行作为各个猫的值,这样的话,用A={1,0,0,0}一乘就是每个毛的ans。

构造单位矩阵的意义就是他们矩阵自己相乘的时候,能够保留自己的值。

这个矩阵很分散,0的那些可以特判掉不枚举多一程O(n)了。这需要你的矩阵乘法是一个一个加上去的,而不是集中一起加上去的。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
struct Matrix {
LL a[maxn][maxn];
int row;
int col;
};
struct Matrix c; //这个要多次用到,栈分配问题,maxn不能开太大,
struct Matrix matrix_mul (struct Matrix a,struct Matrix b,int MOD) {
//求解矩阵a*b%MOD
struct Matrix c = {};
//LL的时候更加是,空间是maxn*maxn的,这样时间用得很多
c.row=a.row; //行等于第一个矩阵的行
c.col=b.col; //列等于第二个矩阵的列
for (int i = ; i <= a.row; ++i) {
for (int k = ; k <= a.col; ++k) {
if (a.a[i][k]) {//应付稀疏矩阵
for (int j = ; j <= b.col; ++j) {
c.a[i][j] += a.a[i][k] * b.a[k][j];
}
}
}
}
return c;
}
struct Matrix quick_matrix_pow (struct Matrix ans,struct Matrix base,int n,int MOD) {
//求解a*b^n%MOD
// cout << n << endl;
while (n) {
if (n&) {
ans=matrix_mul(ans,base,MOD);//传数组不能乱传,不满足交换律
}
n>>=;
base=matrix_mul(base,base,);
}
return ans;
}
int n, m, k;
void work() {
struct Matrix b = {};
b.row = b.col = n;
for (int i = ; i <= n; ++i) {
b.a[i][i] = ;
}
for (int i = ; i <= k; ++i) {
char str[];
int a, c;
scanf("%s", str);
if (str[] == 'g') {
scanf("%d", &a);
++b.a[][a];
} else if (str[] == 'e') {
scanf("%d", &a);
for (int j = ; j <= n; ++j) {
b.a[j][a] = ;
}
} else {
scanf("%d%d", &a, &c);
for (int j = ; j <= n; ++j) {
swap(b.a[j][a], b.a[j][c]);
}
}
}
// for (int i = 0; i <= n; ++i) {
// for (int j = 0; j <= n; ++j) {
// cout << b.a[i][j] << " ";
// }
// cout << endl;
// }
struct Matrix tt = {};
tt.row = ;
tt.col = n;
tt.a[][] = ;
struct Matrix ans = quick_matrix_pow(tt, b, m, );
for (int i = ; i <= n; ++i) {
printf("%lld ", ans.a[][i]);
}
printf("\n");
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (scanf("%d%d%d", &n, &m, &k) != EOF && n + m + k) work();
return ;
}

POJ 3735 Training little cats 矩阵快速幂的更多相关文章

  1. poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化

    题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...

  2. POJ 3735 Training little cats<矩阵快速幂/稀疏矩阵的优化>

    Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13488   Accepted:  ...

  3. poj 3753 Training little cats_矩阵快速幂

    题意: 通过各种操作进行,给第i只猫花生,第i只猫吃光花生,第i只猫和第j只猫互换花生,问n次循环操作后结果是什么 很明显是构建个矩阵,然后矩阵相乘就好了 #include <iostream& ...

  4. 矩阵快速幂 POJ 3735 Training little cats

    题目传送门 /* 题意:k次操作,g:i猫+1, e:i猫eat,s:swap 矩阵快速幂:写个转置矩阵,将k次操作写在第0行,定义A = {1,0, 0, 0...}除了第一个外其他是猫的初始值 自 ...

  5. poj 2888 Magic Bracelet(Polya+矩阵快速幂)

    Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4990   Accepted: 1610 D ...

  6. poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)

    题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...

  7. Training little cats_矩阵快速幂

    Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health ...

  8. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  9. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

随机推荐

  1. Dom vs Canvas (译)

    原文:dom_vs_canvas 在web上做动画我们有很多选择:svg,dom,canvas,flash等等.但是根据业务场景我们一定能找到最优的技术解决方案,但是何时选择dom,何时使用canva ...

  2. ubuntu 安装cuda 成功

    洗洗睡了

  3. wingide 显示中文 及 配色方案

    http://lihuipeng.blog.51cto.com/3064864/923231 网上收集的方法: 显示中文: 任意文本编辑器打开:x:\Wing IDE\bin\gtk-bin\etc\ ...

  4. Python3解leetcode Best Time to Buy and Sell Stock II

    问题描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  5. 解决Response输出时乱码

    Response ServletResponse – 通用的response提供了一个响应应该具有的最基本的属性和方法 | |- HttpServletResponse – 在ServletRespo ...

  6. Maven项目实战(1)

    一.maven的好处? 同样的项目使用maven工程来实现,它的项目源码很小: 1.依赖管理 就是对jar 包管理的过程 2.项目的一键构建 (编译-----测试----- 运行 --打包------ ...

  7. [xdoj1233]Glory and LCS

    题意:求两个排列的最长公共子序列n<=1e5 解题关键:转化为LIS. 最长公共子序列 的 nlogn 的算法本质是 将该问题转化成 最长增序列(LIS),因为 LIS 可以用nlogn实现,所 ...

  8. Deep Learning - Install the Development Environment

    WLS(Windows Subsystem for Linux) Base WLS Installation Guide Initializing a newly installed distro W ...

  9. C++11/14的新特性——更简洁

      新的字符串表示方式——原生字符串(Raw String Literals) C/C++中提供了字符串,字符串的转义序列,给输出带来了很多不变,如果需要原生义的时候,需要反转义,比较麻烦. C++提 ...

  10. 深入剖析ASP.NET Core2.1部署模型,你会大吃一惊

    ----------------------------   以下内容针对 ASP.NET Core2.1版本,2.2推出windows IIS进程内寄宿 暂不展开讨论---------------- ...