Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11496   Accepted: 2815

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 klines 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颗,现有m组重复操作,每组由下面三个中的k个操作组成:
               1. g i 给i只猫咪一颗花生米
               2. e i 让第i只猫咪吃掉它拥有的所有花生米
               3. s i j 将猫咪i与猫咪j的拥有的花生米交换

m次后,每只猫咪有多少颗花生?

可以构建一个1*(n+1)大小的辅助矩阵,即1 0 0 0,然后根据操作构造转置矩阵。

转置矩阵的构造:

转置矩阵一开始初始化为(n+1)*(n+1)大小的单位矩阵,然后每一次操作都要变化。

1.g i 第0行第i列的元素加1

2.e i 第i列的元素都变为0

3.s i j 第i列和第j列的元素都换一下

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0) struct matrix{
ll n,m,i;
ll data[105][105];
void init_danwei(){
for(i=0;i<n;i++){
data[i][i]=1;
}
}
}; matrix multi(matrix &a,matrix &b){
ll i,j,k;
matrix temp;
temp.n=a.n;
temp.m=b.m;
for(i=0;i<temp.n;i++){
for(j=0;j<temp.m;j++){
temp.data[i][j]=0;
}
}
for(i=0;i<a.n;i++){
for(k=0;k<a.m;k++){
if(a.data[i][k]>0){
for(j=0;j<b.m;j++){
temp.data[i][j]=temp.data[i][j]+a.data[i][k]*b.data[k][j];
}
}
}
}
return temp;
} matrix fast_mod(matrix &a,ll n){
matrix ans;
ans.n=a.n;
ans.m=a.m;
memset(ans.data,0,sizeof(ans.data));
ans.init_danwei();
while(n>0){
if(n&1)ans=multi(ans,a);
a=multi(a,a);
n>>=1;
}
return ans;
} int main()
{
ll n,k,m,i,j,e,c,d,h;
while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF)
{
if(n==0 && m==0 && k==0)break;
matrix a;
a.n=a.m=n+1;
memset(a.data,0,sizeof(a.data));
a.init_danwei(); char s[10];
ll temp;
for(i=1;i<=k;i++){
scanf("%s",s);
if(s[0]=='g'){
scanf("%lld",&c);
a.data[0][c]++;
}
else if(s[0]=='s'){
scanf("%lld%lld",&c,&d);
for(j=0;j<n+1;j++){
temp=a.data[j][c];
a.data[j][c]=a.data[j][d];
a.data[j][d]=temp;
}
}
else if(s[0]=='e'){
scanf("%lld",&c);
for(j=0;j<n+1;j++){
a.data[j][c]=0;
} }
} matrix cnt;
cnt=fast_mod(a,m);
matrix ant;
ant.n=1;
ant.m=n+1;
memset(ant.data,0,sizeof(ant.data));
ant.data[0][0]=1; matrix juzhen;
juzhen=multi(ant,cnt); for(i=1;i<=n;i++){
if(i==n)printf("%lld\n",juzhen.data[0][n]);
else printf("%lld ",juzhen.data[0][i]);
} }
return 0;
}

poj3757 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. Training little cats poj3735

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

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

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

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

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

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

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

  7. [POJ3735]Training little cats

    题目:Training little cats 链接:http://poj.org/problem?id=3735 分析: 1)将操作用矩阵表示出来,然后快速幂优化. 2)初始矩阵:$ \left[ ...

  8. POJ 3735:Training little cats 联想到矩阵相乘

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

  9. POJ 3735 Training little cats

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

随机推荐

  1. LeetCode485 最大连续1的个数

    给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...

  2. 手把手教你搭建一个跟vue官方同款文档(vuepress)

    前言 VuePress 由两部分组成:第一部分是一个极简静态网站生成器 (opens new window),它包含由 Vue 驱动的主题系统和插件 API,另一个部分是为书写技术文档而优化的默认主题 ...

  3. centos 6.5 下安装RabbitMQ-3.7.28 二进制版本

    centos 6.5 下安装RabbitMQ-3.7.28 二进制版本 安装依赖: yum install -y ncurses-devel socat logrotatewxWidgets-deve ...

  4. LeetCode530. 二叉搜索树的最小绝对差

    题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...

  5. python异步回调顺序?是否加锁?

    话不多说,直接上代码: import time from functools import partial from concurrent.futures.process import Process ...

  6. Windows10下Canvas对象获得屏幕坐标不正确的原因排查与处理

    因为Canvas没有直接将画布内容保存为图片的方法,所以很多时候是通过获得Canvas画布的坐标,然后通过截图的方式来将画布内容保存为本地图片. 如何取得Canvas画布的坐标呢,比较简单实用的方式如 ...

  7. SparkStreaming和Kafka基于Direct Approach如何管理offset实现exactly once

    在之前的文章<解析SparkStreaming和Kafka集成的两种方式>中已详细介绍SparkStreaming和Kafka集成主要有Receiver based Approach和Di ...

  8. 1V升压5V和1.5V升压5V的集成电路芯片

    1.5V和1V输入,要升压输出5V的集成电路芯片合适? 干电池标准电压是1.5V,放电电压后面在0.9V-1V左右,如果要选用干电池1.5V升压到5V的合适的芯片,需要满足低压1V或者0.9V更好的低 ...

  9. OO第三次总结博客

    规格化设计的发展历史 (因为很难寻找,所以参考了下别的同学的调研结果) 规格化设计与结构化.模块化设计密不可分,伴随着OOP语言的发展,规格化设计思想逐渐形成体系,慢慢完善. 20世纪60年代,程序的 ...

  10. ryu安装

    $ python3 -V Python 3.7.2 $ git clone https://github.com/faucetsdn/ryu.git $ cd ryu $ sudo pip3 inst ...