POJ 3735 Training little cats<矩阵快速幂/稀疏矩阵的优化>
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 13488 | Accepted: 3335 |
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 n, m 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,m,k。(m≤1,000,000,000, n≤100, k≤100),n表示猫的数量,m代表重复的次数,k表示k次操作。
操作种类:
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.
解题过程:
原本以为是一道简单的模拟题,但是m非常的大,会TLE。看了题解,用的是快速幂,第一次写快速幂,做下总结。
http://www.hankcs.com/program/algorithm/poj-3735-training-little-cats-time.html
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=+;
struct node
{
LL a[maxn][maxn];
};
LL n,m,k;
char s[];
LL x,y;
node multiplay(node a,node b)
{
node c;
memset(c.a, , sizeof(c.a));
for(int i=;i<=n+;i++)
for(int j=;j<=n+;j++)
{
if(a.a[i][j])
{
for(int k=;k<=n+;k++)
c.a[i][k]+=a.a[i][j]*b.a[j][k];
}
}
return c;
}
node M(node a,LL x)
{
node c;
memset(c.a, , sizeof(c.a));
for(int i=;i<=n+;i++)
c.a[i][i]=;
while(x)
{
if(x&)
c=multiplay(c, a);
a=multiplay(a, a);
x>>=;
}
return c;
}
int main ()
{
while(~scanf("%lld%lld%lld",&n,&m,&k))
{
if(n==&&m==&&k==) break;
node A;
memset(A.a, , sizeof(A.a));
for(int i=;i<=n+;i++)
A.a[i][i]=;
for(int i=;i<=k;i++)
{
scanf("%s",s);
if(s[]=='g')
{
scanf("%lld",&x);
A.a[x][n+]+=;
}
else if(s[]=='e')
{
scanf("%lld",&x);
memset(A.a[x], , sizeof(A.a[x]));
}
else
{
scanf("%lld%lld",&x,&y);
for(int i=;i<=n+;i++)
swap(A.a[x][i], A.a[y][i]);
}
}
A=M(A,m);
node ans;
memset(ans.a, , sizeof(ans.a));
ans.a[n+][]=;
ans=multiplay(A, ans);
for(int i=;i<=n;i++)
{
if(i==)
printf("%lld",ans.a[i][]);
else
printf("% lld",ans.a[i][]);
}
printf("\n");
}
return ;
}
POJ 3735 Training little cats<矩阵快速幂/稀疏矩阵的优化>的更多相关文章
- 矩阵快速幂 POJ 3735 Training little cats
题目传送门 /* 题意:k次操作,g:i猫+1, e:i猫eat,s:swap 矩阵快速幂:写个转置矩阵,将k次操作写在第0行,定义A = {1,0, 0, 0...}除了第一个外其他是猫的初始值 自 ...
- [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9613 Accepted: 2 ...
- POJ 3735 Training little cats(矩阵快速幂)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11787 Accepted: 2892 ...
- poj 3735 Training little cats(构造矩阵)
http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...
- POJ 3735 Training little cats(矩阵乘法)
[题目链接] http://poj.org/problem?id=3735 [题目大意] 有一排小猫,给出一系列操作,包括给一只猫一颗花生, 让某只猫吃完所有的花生以及交换两只猫的花生, 求完成m次操 ...
- POJ 3735 Training little cats 矩阵快速幂
http://poj.org/problem?id=3735 给定一串操作,要这个操作连续执行m次后,最后剩下的值. 记矩阵T为一次操作后的值,那么T^m就是执行m次的值了.(其实这个还不太理解,但是 ...
- POJ 3735 Training little cats
题意 维护一个向量, 有三种操作 将第\(i\)个数加1 将第\(i\)个数置0 交换第\(i\)个数和第\(j\)个数 Solution 矩阵乘法/快速幂 Implementation 我们将向量写 ...
- poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...
- poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
随机推荐
- javascript中各类的prototype属性
prototype 作用:获取调用对象的对象原型引用 应用:可以为某对象原型添加方法 例: function getMax() { var max = this[0]; for(var x=0; x& ...
- 从urllib和urllib2基础到一个简单抓取网页图片的小爬虫
urllib最常用的两大功能(个人理解urllib用于辅助urllib2) 1.urllib.urlopen() 2. urllib.urlencode() #适当的编码,可用于后面的post提交 ...
- ActiveMQ笔记之点对点队列(Point-to-Point)
1. 点对点通信 点对点是一种一对一通信方式,更像是有一个队列,一个人往队列里放消息,另一个人从队列中取消息,其最大的特点是一个消息只会被消费一次,即使有多个消费者同时消费,他们消费的也是不同的消息. ...
- 基于Node的Web聊天室
1 项目名称 Web聊天室(<这是NodeJs实战>第二章的一个案例,把整个开发过程记录下来)
- D - Binary Lexicographic Sequence URAL - 1081 (贪心)
题目链接:https://cn.vjudge.net/contest/275079#problem/D 具体思路:首先,我们可以观察到1-n位数的种数连起来是一个很有规律的数列,然后我们开始倒序建立. ...
- WordPress浏览数插件的安装使用
插件安装很容易,但是和大多插件都一样,安装后需要调用代码才能显示,我安装后,也调用了.但是就是不显示,后来才发现,我从其他地方复制过来的代码,函数是中文的单引号,这样致使函数失效,注意代码中参数的引号 ...
- Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure y
这个应该是很多github新手经常出错的问题,这个就是没有在你github上添加一个公钥. 下面就直接说步骤: 1 可以用 ssh -T git@github.com去测试一下 图上可以明显看出缺少了 ...
- 动态规划_01背包问题_Java实现
原文地址:http://blog.csdn.net/ljmingcom304/article/details/50328141 本文出自:[梁敬明的博客] 1.动态规划 什么是动态规划?动态规划就是将 ...
- Mysql储存过程8:repeat循环
语法: repeat SQL until 条件 end repeat; 就是相当于其他语言中的: do{ # }while(); mysql> create procedure p1p() -& ...
- 巅峰极客CTF writeup[上]
经验教训 1.CTF不比实战,最好不要死磕.死磕就输了.我就是死磕在缓存文件死的.真的惭愧: 2.对于flag的位置不要太局限于web目录下,如果是命令执行直接上find / -name flag*: ...