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

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

题意是有很多小猫,每个猫一开始有0个花生。

Facer做出g i的动作就是给第i只小猫一个花生。

e i的动作就是让第i只小猫吃掉所有的花生。

s i j的意思是让第i只小猫与第j只小猫交换它们的花生。

这一系列的动作要做m次,问最终每个猫的花生数量。

这个题实际上就是对一个一维数组赋值,交换 经过这样多次的操作之后,问最终这个一位数组的值。

之前没接触过这样的题目,转成二维觉得很新鲜。

g i 就是把第i列的第0行赋值为1。

e i 就是把第i列的所有值赋值为0。

s i j就是交换第i列与第j列的值。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct matrix {
long long m[105][105];
}; long long n, mo, k;
matrix b; matrix mu(matrix no1, matrix no2)
{
matrix t;
memset(t.m, 0, sizeof(t.m)); long long i, j, k; for (i = 0; i <= n; i++)
{
for (k = 0; k <= n; k++)
{
if (no1.m[i][k])
{
for (j = 0; j <= n; j++)
{
t.m[i][j] += no1.m[i][k] * no2.m[k][j];
}
}
}
}
return t; } matrix multi(matrix no, long long x)
{
memset(b.m, 0, sizeof(b.m));
long long i;
for (i = 0; i <= n; i++)
{
b.m[i][i] = 1;
}
while (x > 0)
{
if (x & 1)
b = mu(b, no);
x = x >> 1;
no = mu(no, no);
}
return b;
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); long long i, j, temp;
string test;
matrix no; while (cin >> n >> mo >> k)
{
if (n == 0 && mo == 0 && k == 0)
break;
memset(no.m, 0, sizeof(no.m)); for (i = 0; i <= n; i++)
no.m[i][i] = 1;
for (i = 1; i <= k; i++)
{
cin >> test >> temp;
if (test == "g")
{
no.m[0][temp]++;
}
else if (test == "s")
{
long long temp2;
long long b;
cin >> temp2; for (j = 0; j<= n; j++)
{
b = no.m[j][temp];
no.m[j][temp] = no.m[j][temp2];
no.m[j][temp2] = b;
}
}
else if (test == "e")
{
for (j = 0; j <= n; j++)
{
no.m[j][temp] = 0;
}
}
} no = multi(no, mo); cout << no.m[0][1]; for (i = 2; i <= n; i++)
{
cout << " " << no.m[0][i];
}
cout << endl;
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?
  2. caffe的python接口学习(8):caffemodel中的参数及特征的抽取
  3. nginx 配置管理 - 简单也复杂
  4. WPF:自定义路由事件的实现
  5. Intellij IDEA 14.x 中的Facets和Artifacts的区别
  6. CentOS中查看系统资源占用情况的命令
  7. java比.net优美的一个小地方
  8. sphinx的简单实例
  9. 《将博客搬至CSDN》的文章
  10. android ImageView scaleType属性(转)
  11. 最小费用最大流模板 poj 2159 模板水题
  12. Excel教程(3) - 函数输入方法
  13. &lt;未来世界的幸存者&gt; 读后感(现实篇和职业篇)
  14. jquery系列教程6-ajax的应用全解
  15. Java复习总结——继承
  16. 从GitHub下载demo时遇到的依赖问题
  17. 2018-08-16 中文代码之Spring Boot添加基本日志
  18. windows 驱动开发 MDL 内核层 用户层共享内存
  19. 构建NetCore应用框架之实战篇(七):BitAdminCore框架登录功能源码解读
  20. ManualResetEvent

热门文章

  1. iOS开发的调试技巧
  2. Lesson 9 Royal espionage
  3. 常用WinAPI函数整理------------转载
  4. Java垃圾回收机制详解和调优
  5. redis学习笔记-02:为什么使用NoSQL数据库
  6. 与(&amp;)、非(~)、或(|)、异或(^)
  7. Pyspider的基本使用 -- 入门
  8. centos7 root下创建系统时间同步定时任务
  9. javascript中,对象本身就是一种Map结构。
  10. java 根据传入的时间获取当前月的第一天的0点0分0秒和最后一天的23点59分59秒