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) ...
随机推荐
- Java实现JsApi方式的微信支付
要使用JsApi进行微信支付,首先要从微信获得一个prepay_id,然后通过调用微信的jsapi完成支付,JS API的返回结果get_brand_wcpay_request:ok仅在用户成功完成支 ...
- 你必须了解Spring的生态
Spring不止是提供了IOC.AOP的功能,还提供了大量的基于Spring的项目,拿来用就行了,用于一站式开发,大大降低了开发的难度. 下面列举下主要的一些Spring的生态项目: Spring B ...
- 【BZOJ】1875: [SDOI2009]HH去散步 矩阵快速幂
[题意]给定n个点m边的无向图,求A到B恰好经过t条边的路径数,路径须满足每条边都和前一条边不同.n<=20,m<=60,t<=2^30. [算法]矩阵快速幂 [题解]将图的邻接矩阵 ...
- python作业Select版本FTP(第十周)
SELECT版FTP: 使用SELECT或SELECTORS模块实现并发简单版FTP 允许多用户并发上传下载文件 思路解析: 1. 使用IO多路复用的知识使用SELECTORS封装好的SELECTOR ...
- B - GuGuFishtion(莫比乌斯 欧拉函数 预处理mu函数的欧拉函数的模板)
题目链接:https://cn.vjudge.net/contest/270608#problem/B 题目大意:题目中说,就是对欧拉函数的重新定义的一种函数的求和. 证明方法: AC代码: #inc ...
- vim 颜色主题设置
先看看vim编辑器提供的色彩配置方案: 首先进入vim的color目录(/usr/share/vim/vim74/colors,不同的系统目录不同,建议在-/建立.vim目录,然后在些目录里建立对应的 ...
- Django框架<一>
Django框架 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Sess ...
- numpy 简介
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- makefile里PHONY的相关介绍
Phony Targets PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字.有两种理由需要使用PHONY 目标:避免和同名文件冲突,改善性能. 如果编写一个规则,并不产生目标文件 ...
- sea.js中的checkbox批量操作
<table width="100%" border="0" cellspacing="0" cellpadding="0& ...