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. Matlab boxplot for Multiple Groups(多组数据的箱线图)

    在画之前首先介绍一下Matlab boxplot,下面这段说明内容来自http://www.plob.org/2012/06/10/2153.html   由于matlab具有强大的计算功能,用其统计 ...

  2. Java 正则表达式 向前、向后匹配

    //向后匹配 String a = "I paid $90 for 10 oranges, 12 pears and 8 apples. I saved $5 on "; Patt ...

  3. 在浏览器中将表格导入到本地的EXCEL文件,注意控制内存

    if ($export_flag == 1) { $rr = $this->mdl->test($test); header("Content-Type: application ...

  4. 为什么 Node.js 这么火,而同样异步模式 Python 框架 Twisted 却十几年一直不温不火?

    twisted是一个强大的异步网络框架,应用的面也非常广,但是没有这几年才出现的Node.js火,社区.文档也是很少可怜我觉得二者其实在本质上差不多,而且python使用起来还是比较容易一些的 匿名用 ...

  5. 查询使用NoLock

    当我们在操作数据库的时候,无论是查询还是修改数据库的操作我们都习惯使用using(var db=new XXXDB()){},但是如果仅仅是做查询,最好是使用NoLock,因为NoLock使用的是共享 ...

  6. 使用C#下载网络文件

    下载 /// <summary> /// 下载文件 /// </summary> /// <param name="URL">下载文件地址< ...

  7. oracle查询语句大全

    1.查处oracle数据库的字符编码格式 select * from v$nls_parameters t where t.PARAMETER='NLS_CHARACTERSET'; 如果查出的是ZH ...

  8. sql删除多余重复的数据只保留一条

    delete from people where   peopleName in (select peopleName    from people group by peopleName      ...

  9. HDU 4906 Our happy ending(2014 Multi-University Training Contest 4)

    题意:构造出n个数 这n个数取值范围0-L,这n个数中存在取一些数之和等于k,则这样称为一种方法.给定n,k,L,求方案数. 思路:装压 每位 第1为表示这种方案能不能构成1(1表示能0表示不能)   ...

  10. JSON:org.json的基本用法

    java中用于解释json的主流工具有org.json.json-lib与gson,本文介绍org.json的应用. 官方文档: http://www.json.org/java/ http://de ...