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. hdu 1561 The more, The Better (树上背包)

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. Objective-C:Foundation框架-常用类-NSValue

    NSNumber是NSValue的子类,前者只能包装数字,后者可以包装任意值.NSArray.NSDictionary只能存储OC对象,不能存储结构体.因此,如果想要在NSArray.NSDictio ...

  3. ajax使用jquery的实现方式

    1.jquery的ajax方法. $("#ajaxbtn").click(function(){ $.ajax({ url:"json.do", beforeS ...

  4. Masonry使用案列详解

    案例一: 要求:无论在什么尺寸的设备上(包括横竖屏切换),红色view都居中显示.

  5. 如何在Quagga BGP路由器中设置IPv6的BGP对等体和过滤

    在本教程中,我们会向你演示如何创建IPv6 BGP对等体并通过BGP通告IPv6前缀.同时我们也将演示如何使用前缀列表和路由映射特性来过滤通告的或者获取到的IPv6前缀. 拓扑 服务供应商A和B希望在 ...

  6. POJ 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

  7. HDU 1828 扫描线(矩形周长并)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. 2.精通前端系列技术之seajs模块化使工作更简单(二)

    drag.js // JavaScript Document //B开发 define(function(require,exports,module){ function drag(obj){ ; ...

  9. inoic是什么

    本篇只侧重框架提供的功能和能力的研究,请关注后续实际部署使用体验. 一.inoic是什么? inoic是一个可以使用Web技术以hybird方式开发移动app的前端开源框架. 二.inoic框架特点 ...

  10. tableview_nav 动画效果

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat yOffset  = scrollView.contentOffset. ...