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. VC Q&A (原创)

    Q1:External Dependencies有什么作用? A1:(网友答复:)External   Dependencies是说你没有把这个文件加入到这个工程中,但是需要这个文件的支持.当然有时是 ...

  2. Chapter2 二分与三分

    T1 给一个N个数的序列,分成M段,每段最大值最小 sol:二分最大值,贪心Check T2 平面上n个点,每个点每s会向周围扩散一个单位长度,两个点联通当且仅当扩散有交点,问什么时候这n个点联通 s ...

  3. P4311 士兵占领[最大流]

    题目地址 有一个$M * N$的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了$L_ ...

  4. [SHOI 2017] 分手是祝愿

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4872 [算法] 首先发现 , 对于一个开关 , 按下2次和没按是等价的 , 因此每个 ...

  5. BZOJ3219:巡游

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...

  6. ubuntu下mysql的安装与配置

    1.安装,安装的过程中会提示你设置 MySql的"root"密码 sudo apt-get install mysql-server mysql-client 2.把  /etc/ ...

  7. C# 32位程序在64位系统下运行中解决重定向问题

    在64位的Windows操作系统中,为了兼容32位程序的运行,64位的Windows操作系统采用重定向机制.目的是为了能让32位程序在64位的操作系统不仅能操作关键文件文夹和关键的注册表并且又要避免与 ...

  8. Http协议-URI和资源

    所有东西都有一个标准化的名字,以帮助人们寻找城市中的各种资源.书籍有ISBN号,公交车有线路号,银行账户有账户编码,人有身份证,街道有街道名称.人们告诉图书馆管理员书籍的ISBN号,他即可找出该书籍的 ...

  9. 32bit / 64bit co-exist Linux, ld-linux.so, linux-gate.so.1 etc

    before this, confirm that you don't have 32bit libs notably 32bit libc, e.g. you have /lib64/ld-linu ...

  10. JDK的windows和Linux版本之下载

    简单说下,Eclipse需要Jdk,MyEclipse有自带的Jdk,除非是版本要求 http://www.oracle.com/technetwork/java/javase/downloads/i ...