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

【题意】有n只猫咪,每只猫咪有0花生,g x表示给第x只猫咪一颗花生,e x表示第x只猫咪把花生全吃了,s x y表示交换x和y 猫咪的花生数;

将上述k次操作进行m次,求最后每只猫咪的花生数。

【思路】由于m的数非常大,所以一般的方法肯定会Tel,所以用矩阵快速幂

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<cstdlib>
#include<map>
using namespace std;
const int N=;
long long int n,m,k;
struct Mat
{
long long int mat[N][N];
void clear()
{
memset(mat,,sizeof(mat));
}
void init()
{
clear();
for(int i=;i<=n;i++)
{
mat[i][i]=;
}
}
};
Mat a;
Mat mul(Mat a,Mat b)//矩阵乘法
{
Mat c;
c.clear();//刚开始把c.init()改了好久
for(int i=;i<=n;i++)
{
for(int k=;k<=n;k++)
{
if(a.mat[i][k])
{
for(int j=;j<=n;j++)
{
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
}
}
}
}
return c;
}
Mat pow(Mat a,long long int m)//快速幂
{
if(m==) return a;
Mat c;
c.init();
while(m)
{
if(m&) c=mul(c,a);
m>>=;
a=mul(a,a);
}
return c; } int main()
{
while(~scanf("%lld%lld%lld",&n,&m,&k))
{
if(!n&& !m&& !k) break;
a.init();
while(k--)
{
long long int x,y;
char op[];
scanf("%s",op);
if(op[]=='g')
{
scanf("%lld",&x);
a.mat[][x]++;
}
else if(op[]=='e')
{
scanf("%lld",&x);
for(int i=;i<=n;i++)
{
a.mat[i][x]=;
}
}
else
{
scanf("%lld%lld",&x,&y);
for(int i=;i<=n;i++)
{
swap(a.mat[i][x],a.mat[i][y]);
}
}
}
if(m==)
{
printf("");
for(int i=;i<=n;i++)
{
printf("");
}
printf("\n");
continue;
} a=pow(a,m);
printf("%lld",a.mat[][]);
for(int i=;i<=n;i++)
{
printf(" %lld",a.mat[][i]);
}
printf("\n"); }
return ;
}

Training little cats_矩阵快速幂的更多相关文章

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

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

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

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

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

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

  4. hdu4686 Arc of Dream 2013 Multi-University Training Contest 9矩阵快速幂

    Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Tot ...

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

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

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

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

  7. Training little cats(poj3735,矩阵快速幂)

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

  8. 2014 Super Training #10 G Nostop --矩阵快速幂

    原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...

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

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

随机推荐

  1. 如何实现301的跳转?当输入域名http://xxx.com的时候自动重定向到www上去

    答案:在服务器上操作,注意勾选和不勾选的区别,使用Fiddle进行观察,301和302之间的区别

  2. [Js]Ajax

    一.什么是Ajax 不刷新的情况下读取数据或提交数据 (最早出现ajax:谷歌地图,拖动一下出现一片新的视野) 应用:用户注册.在线聊天.微博 特性:只能从服务器上去读取数据(所以我们需要配置自己的服 ...

  3. centos下cmake安装

    步骤一.安装gcc等必备程序包(已安装则略过此步,用gcc -v检测) yum install -y gcc gcc-c++ make automake 步骤二.安装wget (已安装则略过此步) y ...

  4. php解密java的DES加密

    echo openssl_decrypt( $密文 ,"des-ecb" , $密钥 );

  5. 从QQ网站中提取的纯JS省市区三级联动

    在 http://ip.qq.com/ 的网站中有QQ自己的JS省市区三级联动 QQ是使用引用外部JS来实现三级联动的.JS如下:http://ip.qq.com/js/geo.js <!DOC ...

  6. $where $options: 'g','i'

    db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,false) ...

  7. java枚举类

    enum关键字用于定义枚举类,若枚举只有一个成员, 则可以作为一种单例模式的实现方式.   枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰. 枚举类的使用 priva ...

  8. MonoRail学习-入门实例篇

    1.到官方网站下载安装文件,地址如下: http://www.castleproject.org/index.php/Castle:Download目前最新版本Beta5(您也可以不需要下载,直接使用 ...

  9. [Swift2.0系列]Defer/Guard 基础语法

    1.Defer Swift2.0中加入了defer新语法声明.defer译为延缓.推迟之意.那么在Swift2.0中它将被应用于什么位置呢?比如,读取某目录下的文件内容并处理数据,你需要首先定位到文件 ...

  10. 在网页中编辑报表的报表设计器Stimulsoft Reports Designer.Web报表控件

    Stimulsoft Reports Designer.Web报表控件是一款网页报表设计器.您想在网页中编辑您的报表吗?现在是可能的! Stimulsoft Reports Designer.Web ...