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. Jmeter-JDBC Request

    1.  新建一个测试计划 2.  新建一个线程组 3.  创建数据库连接 4.配置数据库连接 5.添加JDBC Request 6.添加监听器

  2. Go丨语言对数据库操作报错 panic: dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it.

    panic: dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine ac ...

  3. 网络编程学习笔记-TCP拥塞控制机制

    为了防止网络的拥塞现象,TCP提出了一系列的拥塞控制机制.最初由V. Jacobson在1988年的论文中提出的TCP的拥塞控制由“慢启动(Slow start)”和“拥塞避免(Congestion ...

  4. 从结果推断过程----->使用System.out和Root Device

    刚才解决了一个App中更新的逻辑问题.出问题之后发现,有很多处调用了更新,后来都不知道是哪里改写了SharedPreferences. 然后一直在挨个寻找每一处更新的地方,花了很多时间. 最后直接使用 ...

  5. 解决系统存在大量TIME_WAIT状态的连接

    系统存在大量TIME_WAIT状态的连接,通过调整内核参数解决, vi /etc/sysctl.conf 编辑文件,加入以下内容:net.ipv4.tcp_syncookies = 1net.ipv4 ...

  6. Gym-101673: A Abstract Art (模板,求多个多边形的面积并)

    手抄码板大法. #include<bits/stdc++.h> using namespace std; #define mp make_pair typedef long long ll ...

  7. 1139 First Contact(30 分)

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  8. Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)

    前言:本项目基于spring4.x构建,使用ehcache3.5.2和JCache(jsr107规范) 一.依赖 除了ehcache和cache-api外,注意引用spring-context-sup ...

  9. POJ3928(树状数组:统计数字出现个数)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2641   Accepted: 978 Descript ...

  10. Python 同ip网站查询(制作网站接口)

    老师专门打电话回来说不让玩电脑~~   呵呵.  LOL把被人坑死了. 五排果然不适合我. 去翻了下曾经的Python项目. 在爬虫文件夹下面找到了这个. 我也就厚脸皮的什么都不改就放上来了: #co ...