Training little cats poj3735
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 9299 | Accepted: 2230 |
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
【题意】:有n只猫咪,开始时每只猫咪有花生0颗,现有一组操作,由下面三个中的k个操作组成:
1. g i 给i只猫咪一颗花生米
2. e i 让第i只猫咪吃掉它拥有的所有花生米
3. s i j 将猫咪i与猫咪j的拥有的花生米交换
现将上述一组操作做m次后,问每只猫咪有多少颗花生?
【题解】:m达到10^9,显然不能直接算。
因为k个操作给出之后就是固定的,所以想到用矩阵,矩阵快速幂可以把时间复杂度降到O(logm)。问题转化为如何构造转置矩阵?
说下我的思路,观察以上三种操作,发现第二,三种操作比较容易处理,重点落在第一种操作上。
有一个很好的办法就是添加一个辅助,使初始矩阵变为一个n+1元组,编号为0到n,下面以3个猫为例:
定义初始矩阵A = [1 0 0 0],0号元素固定为1,1~n分别为对应的猫所拥有的花生数。
对于第一种操作g i,我们在单位矩阵基础上使Mat[0][i]变为1,例如g 1:
1 1 0 0
0 1 0 0
0 0 1 0
0 0 0 1,显然[1 0 0 0]*Mat = [1 1 0 0]
对于第二种操作e i,我们在单位矩阵基础使Mat[i][i] = 0,例如e 2:
1 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1, 显然[1 2 3 4]*Mat = [1 2 0 4]
对于第三种操作s i j,我们在单位矩阵基础上使第i列与第j互换,例如s 1 2:
1 0 0 0
0 0 0 1
0 0 1 0
0 1 0 0,显然[1 2 0 4]*Mat = [1 4 0 2]
现在,对于每一个操作我们都可以得到一个转置矩阵,把k个操作的矩阵相乘我们可以得到一个新的转置矩阵T。
A * T 表示我们经过一组操作,类似我们可以得到经过m组操作的矩阵为 A * T ^ m,最终矩阵的[0][1~n]即为答案。
上述的做法比较直观,但是实现过于麻烦,因为要构造k个不同矩阵。
有没有别的方法可以直接构造转置矩阵T?答案是肯定的。
我们还是以单位矩阵为基础:
对于第一种操作g i,我们使Mat[0][i] = Mat[0][i] + 1;
对于第二种操作e i,我们使矩阵的第i列清零;
对于第三种操作s i j,我们使第i列与第j列互换。
这样实现的话,我们始终在处理一个矩阵,免去构造k个矩阵的麻烦。
至此,构造转置矩阵T就完成了,接下来只需用矩阵快速幂求出 A * T ^ m即可,还有一个注意的地方,该题需要用到long long。
具体实现可以看下面的代码。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 110
typedef struct abcd
{
long long a[maxn][maxn];
} abcd;
abcd a,b;
int n;
void mul(abcd x,abcd &y)
{
abcd z;
memset(z.a,,sizeof(z.a));
int i,j,k;
for(k=; k<=n; k++)
for(i=; i<=n; i++)
if(x.a[i][k])
for(j=; j<=n; j++)
z.a[i][j]+=x.a[i][k]*y.a[k][j]; for(i=; i<=n; i++)
for(j=; j<=n; j++)y.a[i][j]=z.a[i][j];
}
void fun(long long m)
{
int i,j;
memset(b.a,,sizeof(b.a));
for(i=; i<=n; i++)
b.a[i][i]=;
while(m)
{
if(m&)
{
mul(a,b);
}
m>>=;
mul(a,a);
}
}
int main()
{
int k,i,y,yy,j;
int m;
char x;
while(scanf("%d%d%d",&n,&m,&k),(n||m||k))
{
memset(a.a,,sizeof(a.a));
for(i=; i<=n; i++)a.a[i][i]=;
for(i=; i<k; i++)
{
getchar();
x=getchar();
if(x=='g')
{
scanf("%d",&y);
a.a[][y]++;
}
else if(x=='s')
{
scanf("%d%d",&y,&yy);
for(j=; j<=n; j++)swap(a.a[j][y],a.a[j][yy]);
}
else if(x=='e')
{
scanf("%d",&y);
for(j=; j<=n; j++)a.a[j][y]=;
}
}
fun(m);
for(i=; i<n; i++)printf("%I64d ",b.a[][i]);
printf("%I64d\n",b.a[][i]);
}
}
Training little cats poj3735的更多相关文章
- Training little cats(poj3735,矩阵快速幂)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10737 Accepted: ...
- [POJ3735]Training little cats
题目:Training little cats 链接:http://poj.org/problem?id=3735 分析: 1)将操作用矩阵表示出来,然后快速幂优化. 2)初始矩阵:$ \left[ ...
- 矩阵快速幂 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: 13488 Accepted: ...
- POJ 3735 Training little cats(矩阵快速幂)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11787 Accepted: 2892 ...
- POJ 3735:Training little cats 联想到矩阵相乘
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11208 Accepted: ...
- xiaowuga poj3735—Training little cats(特殊操作转化为矩阵操作)
题意:有n只猫,对其进行k次操作,然后反复这样操作m次. 其中g 表示 i 猫加1, e表示 i 猫为0:s表示 i 与 j 猫互换. 解释一下样例: 3 1 6g 1g 2g 2s 1 2g 3e ...
- poj3735—Training little cats(特殊操作转化为矩阵操作)
题目链接:http://poj.org/problem?id=3735 题目意思: 调教猫咪:有n只饥渴的猫咪,现有一组羞耻连续操作,由k个操作组成,全部选自: 1. g i 给第i只猫咪一颗花生 2 ...
随机推荐
- 常用Dos操作指令
1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含文件. /w:以紧凑方式(一行显示5个 ...
- Microsoft Dynamics 365 之 味全食品 项目分享和Customer Engagement新特性分享
味全食品 Dynamics 365项目: 在企业门户和电子商务等新营销模式频出的今天,零售业需要利用统一的管理平台管理日益庞大的客户及销售数据,整合线上线下的零售业务,从采购.仓储.生产.配送到销售. ...
- SQL Server 常用操作XML
--修改FunctionNo节点值,@OperateFunctionNo为参数 set @DataXml.modify('replace value of (/CrudData/FunctionNo/ ...
- 锤子坚果pro突破京东10万好评,还有什么是锤子科技做不出...
作为2017年手机圈明星产品的锤子科技坚果Pro,可是一点都不安分呢! 9月份锤子科技推出一款新机型--坚果Pro银魂定制版 这在动漫圈跟手机圈都掀起一股不小的浪潮. 外观漂亮,颜值超高,续航时间长. ...
- YYHS-挑战nbc
题目描述 Abwad是一名有志向的优秀OI少年.遗憾的是,由于高能宇宙射线的影响,他不幸在NOI中滚粗.不过,Abwad才高一,还有许许多多的机会.在长时间的刻苦学习之后,他实力大增,并企图撼动OI界 ...
- STEP 7-MicroWIN SMART 西门子PLC再次安装问题
我的电脑第一次安装s7(STEP 7-MicroWIN SMART 西门子PLC)是没有问题的,有一次不小心删除,再次安装却怎么也安装不上.猫那个咪的!Why! 网上各种查资料,完全不能解决问题,有的 ...
- NHibernate教程(5)--CRUD操作
NHibernate之旅(5):探索Insert, Update, Delete操作 2008-10-17 16:31 by 李永京, 42903 阅读, 73 评论, 收藏, 编辑 本节内容 操作 ...
- mpls vpn剩余笔记
将IP地址映射为简单的具有固定长度的标签 用于快速数据包交换 20 3 1 8 在整个转发过程中,交换节点仅根据标记进行转发 标签交换路径(LSP) 多协议标签交换MPLS最初是为了提高转发速度而提出 ...
- java课程设计--We Talk(201521123061)
java课程设计--We Talk(201521123061) 团队博客链接:http://www.cnblogs.com/slickghost/ 数据库 一.通过Dao模式建立与数据库的连接 1.数 ...
- 201521123039 《java程序设计》第九周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? ...