Training little cats
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9613   Accepted: 2296

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

Source


题目大意:
有N仅仅猫,K次操作(得花生、吃花生、交换花生),反复M次。

问最后每仅仅猫各有多少花生剩余。


解题思路:
这道题目的巧妙之处就是构造单位矩阵。从而实现三种操作。


之后便能用矩阵高速幂了。

另外还有两个trick。
1. 由于矩阵的乘法是O(N^3)。复杂度高。这道题直接搞会T。而我们能够发现,矩阵乘法都是稀疏矩阵,稀疏矩阵乘法有优化。
/*
稀疏矩阵乘法优化
*/
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (!a[i][j]) continue; //稀疏矩阵乘法优化
for (int k = 0; k < b.m; k++)
tmp.a[i][k] += a[i][j] * b.a[j][k];
}

2. 尽管M,N,K都是 int 范围的,可是操作之后,每仅仅猫的花生数量会超 int。


代码:
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
const int maxn = 110;
const int maxm = 110;
const int mod = 10000;
struct Matrix {
int n, m;
ll a[maxn][maxm];
void clear() {
n = m = 0;
memset(a, 0, sizeof(a));
}
Matrix operator * (const Matrix &b) const {
Matrix tmp;
tmp.clear();
tmp.n = n; tmp.m = b.m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (!a[i][j]) continue; //稀疏矩阵乘法优化
for (int k = 0; k < b.m; k++)
tmp.a[i][k] += a[i][j] * b.a[j][k];
}
return tmp;
}
};
int n, k, m;
void init(int n, Matrix &I) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
I.a[i][j] = 0LL;
}
I.a[i][i] = 1LL;
}
I.n = I.m = n + 1; }
Matrix Matrix_pow(Matrix A, int k) {
Matrix res;
init(n, res);
while(k) {
if (k & 1) res = res * A;
k >>= 1;
A = A * A;
}
return res;
}
int main () {
Matrix A, I, res;
while(scanf("%d%d%d", &n, &m, &k)) {
if (m == 0 && n == 0 && k == 0) {
break;
}
A.clear();
A.n = n + 1; A.m = 1;
A.a[n][0] = 1LL;
char ch;
int u, v;
init(n, res);
while(k--) {
init(n, I);
getchar();
scanf("%c", &ch);
if (ch == 'g') {
scanf("%d", &u);
I.a[u - 1][n] = 1LL;
}
if (ch == 's') {
scanf("%d%d", &u, &v);
I.a[u - 1][u - 1] = 0LL; I.a[u - 1][v - 1] = 1LL;
I.a[v - 1][v - 1] = 0LL; I.a[v - 1][u - 1] = 1LL;
}
if (ch == 'e') {
scanf("%d", &u);
I.a[u - 1][u - 1] = 0LL;
}
res = I * res; //每次操作时。I应该乘在前面,由于矩阵乘法没有交换律
}
Matrix tmp = Matrix_pow(res, m);
tmp = tmp * A;
for (int i = 0; i < n; i++) {
printf("%lld ", tmp.a[i][0]);
}
cout<<endl;
}
return 0;
}

上述图片来自:http://blog.csdn.net/magicnumber/article/details/6217602

版权声明:本文博主原创文章,博客,未经同意不得转载。

[POJ 3735] Training little cats (结构矩阵、矩阵高速功率)的更多相关文章

  1. poj 3735 Training little cats(构造矩阵)

    http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...

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

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

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

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

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

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

  5. POJ 3735 Training little cats(矩阵乘法)

    [题目链接] http://poj.org/problem?id=3735 [题目大意] 有一排小猫,给出一系列操作,包括给一只猫一颗花生, 让某只猫吃完所有的花生以及交换两只猫的花生, 求完成m次操 ...

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

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

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

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

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

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

  9. POJ 3735 Training little cats

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

随机推荐

  1. OpenCV Python教程(3、直方图的计算与显示)

    转载请详细注明原作者及出处,谢谢! 本篇文章介绍如何用OpenCV Python来计算直方图,并简略介绍用NumPy和Matplotlib计算和绘制直方图 直方图的背景知识.用途什么的就直接略过去了. ...

  2. JDBC使用数据库来完成分页功能

    本篇讲诉如何在页面中通过操作数据库来完成数据显示的分页功能.当一个操作数据库进行查询的语句返回的结果集内容如果过多,那么内存极有可能溢出,所以在大数据的情况下分页是必须的.当然分页能通过很多种方式来实 ...

  3. Spark Sreaming与MLlib机器学习

    Spark Sreaming与MLlib机器学习 本来这篇是准备5.15更的,但是上周一直在忙签证和工作的事,没时间就推迟了,现在终于有时间来写写Learning Spark最后一部分内容了. 第10 ...

  4. nyoj 55 懒省事的小明 优先队列 multiset 还有暴力

    懒省事的小明 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 3   描述       小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的 ...

  5. Element.Event

    addEvent(type,fn):为DOM元素增加一个事件监听器 removeEvent(type,fn):移除先前为DOM元素添加的事件监听器 eg: var destroy = function ...

  6. 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序

    三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...

  7. 【Demo 0005】视图控制器

    本章学习要点:       1.  熟悉MVC模型及模型中(Modal, View, Control)用途:       2.  了解iOS中常见的几种视图控制器及使用场景:       3.  掌握 ...

  8. script:查看redo产生的历史信息

    script:查看redo产生的历史信息 SQL> alter session set nls_date_format='dd-mon-yy'; SQL>  set lines 160 p ...

  9. Spring MVC 数据验证——validate注解方式

    1.说明 学习注解方式之前,应该先学习一下编码方式的spring注入.这样便于理解验证框架的工作原理.在出错的时候,也能更好的解决这个问题.所以本次博客教程也是基于编码方式.仅仅是在原来的基础加上注解 ...

  10. 3TB硬盘的容量已经超出了传统分区标准的支持

    为什么3TB会有接近750G空间不能用? MBR分区格式是瓶颈 其实3TB硬盘之所以会出现各种问题,关键就在于它的容量已经超出了传统分区标准的支持.传统的硬盘采用MBR分区格式,使用LBA寻址,这种寻 ...