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

Description

Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer has well designed a set of moves for his cats. He is now asking you to supervise the cats to do his exercises. Facer's great exercise for cats contains three different moves:
g i : Let the ith cat take a peanut.
e i : Let the ith cat eat all peanuts it have.
s i j : Let the ith cat and jth cat exchange their peanuts.
All
the cats perform a sequence of these moves and must repeat it m times!
Poor cats! Only Facer can come up with such embarrassing idea. 
You
have to determine the final number of peanuts each cat have, and
directly give them the exact quantity in order to save them.

Input

The input file consists of multiple test cases, ending with three zeroes "0 0 0". For each test case, three integers nm and k are given firstly, where n is the number of cats and k is the length of the move sequence. The following k lines describe the sequence.
(m≤1,000,000,000, n≤100, k≤100)

Output

For each test case, output n numbers in a single line, representing the numbers of peanuts the cats have.

Sample Input

3 1 6
g 1
g 2
g 2
s 1 2
g 3
e 2
0 0 0

Sample Output

2 0 1

Source

/*
题意:多组输入n,m,k。(m≤1,000,000,000, n≤100, k≤100),n表示猫的数量,m代表重复的次数,k表示k次操作。
操作种类:
g i : Let the ith cat take a peanut.
e i : Let the ith cat eat all peanuts it have.
s i j : Let the ith cat and jth cat exchange their peanuts.
解题过程:
原本以为是一道简单的模拟题,但是m非常的大,会TLE。看了题解,用的是快速幂,第一次写快速幂,做下总结。
http://www.hankcs.com/program/algorithm/poj-3735-training-little-cats-time.html
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=+;
struct node
{
LL a[maxn][maxn];
};
LL n,m,k;
char s[];
LL x,y;
node multiplay(node a,node b)
{
node c;
memset(c.a, , sizeof(c.a));
for(int i=;i<=n+;i++)
for(int j=;j<=n+;j++)
{
if(a.a[i][j])
{
for(int k=;k<=n+;k++)
c.a[i][k]+=a.a[i][j]*b.a[j][k];
}
}
return c;
}
node M(node a,LL x)
{
node c;
memset(c.a, , sizeof(c.a));
for(int i=;i<=n+;i++)
c.a[i][i]=;
while(x)
{
if(x&)
c=multiplay(c, a);
a=multiplay(a, a);
x>>=;
}
return c;
}
int main ()
{
while(~scanf("%lld%lld%lld",&n,&m,&k))
{
if(n==&&m==&&k==) break;
node A;
memset(A.a, , sizeof(A.a));
for(int i=;i<=n+;i++)
A.a[i][i]=;
for(int i=;i<=k;i++)
{
scanf("%s",s);
if(s[]=='g')
{
scanf("%lld",&x);
A.a[x][n+]+=;
}
else if(s[]=='e')
{
scanf("%lld",&x);
memset(A.a[x], , sizeof(A.a[x]));
}
else
{
scanf("%lld%lld",&x,&y);
for(int i=;i<=n+;i++)
swap(A.a[x][i], A.a[y][i]);
}
}
A=M(A,m);
node ans;
memset(ans.a, , sizeof(ans.a));
ans.a[n+][]=;
ans=multiplay(A, ans);
for(int i=;i<=n;i++)
{
if(i==)
printf("%lld",ans.a[i][]);
else
printf("% lld",ans.a[i][]);
}
printf("\n");
}
return ;
}

POJ 3735 Training little cats<矩阵快速幂/稀疏矩阵的优化>的更多相关文章

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

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

  2. [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)

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

  3. POJ 3735 Training little cats(矩阵快速幂)

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

  4. poj 3735 Training little cats(构造矩阵)

    http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...

  5. POJ 3735 Training little cats(矩阵乘法)

    [题目链接] http://poj.org/problem?id=3735 [题目大意] 有一排小猫,给出一系列操作,包括给一只猫一颗花生, 让某只猫吃完所有的花生以及交换两只猫的花生, 求完成m次操 ...

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

    http://poj.org/problem?id=3735 给定一串操作,要这个操作连续执行m次后,最后剩下的值. 记矩阵T为一次操作后的值,那么T^m就是执行m次的值了.(其实这个还不太理解,但是 ...

  7. POJ 3735 Training little cats

    题意 维护一个向量, 有三种操作 将第\(i\)个数加1 将第\(i\)个数置0 交换第\(i\)个数和第\(j\)个数 Solution 矩阵乘法/快速幂 Implementation 我们将向量写 ...

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

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

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

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

随机推荐

  1. 使用HTML5、CSS3和jQuery增强网站用户体验[留存]

    记得几年前如果你需要添加一些互动元素到你的网站中用来改善用户体验?是不是立刻就想到了flash实现?这彷佛年代久远的事了.使用现在最流行的Web技术HTML5,CSS3和jQuery,同样也可以实现类 ...

  2. 【洛谷 P3199】 [HNOI2009]最小圈(分数规划,Spfa)

    题目链接 一开始不理解为什么不能直接用\(Tarjan\)跑出换直接求出最小值,然后想到了"简单环",恍然大悟. 二分答案,把所有边都减去\(mid\),判是否存在负环,存在就\( ...

  3. webpack4 未设置mode会自动压缩

    最近想用LayaBox做个小游戏,然而Laya本身不自带构建工具.然后觉得写模块化的东西还是用webpack好使,用es6的语法也比较清晰. 于是就装了webpack,只用babel-loader来编 ...

  4. JS设计模式——1.富有表现力的JS

    创建支持链式调用的类(构造函数+原型) Function.prototype.method = function(name, fn){ this.prototype[name] = fn; retur ...

  5. Linux 下解决安装多个node冲突的问题(重新安装node)

    一个系统中不经意安装了多个node版本,结果更新后还是原来的版本,下面思考一下解决办法: 敲黑板: 1. nodejs 用 包管理器安装一般在 /usr/local/bin 2. 查看当前目录下的no ...

  6. git clone的

    git clone git@e.coding.net:wudi360/*******.git

  7. Linux命令参数处理 shell脚本函数getopts

    getopts 命令 用途 处理命令行参数,并校验有效选项. 语法 getopts 选项字符串 名称 [ 参数 ...] 描述 getopts 的设计目标是在循环中运行,每次执行循环,getopts ...

  8. WireShark出现The NPF driver isn't running的问题

    昨天开始尝试装上了wireshark网络监视软件,可是今天打开去总是出现“The NPF driver isn't running.You may have trouble capturing or ...

  9. curd 插件

    1. Django项目启动 自动加载文件 制作启动文件 . 注册strak 在apps.py 类里面增加如下 def ready(self): from django.utils.module_loa ...

  10. C语言实现knn

    以后写代码一定要谨慎,提高代码的正确率. /*************************************** * 1.初始化距离为最大值 * 2.计算未知样本和每个训练样本的距离为dis ...