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) ...
随机推荐
- ④ 设计模式的艺术-10.装饰(Decorator)模式
职责 装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 装饰模式是一种用于代替继承的技术,无需通过继承增加子类就能扩展对 ...
- 【不能继续浪啦】BZ做题记录[7.01~7.06]
距离上次提交..><居然已经过去一个半月了... 然后再去看看人家RXDoi.. 差距越来越大啦... 最后更新时间:7.06 19:06 [07.03 21:02]夏令营自修课逃逃真爽. ...
- 【AtCoder】ARC092 D - Two Sequences
[题目]AtCoder Regular Contest 092 D - Two Sequences [题意]给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1<=i,j<=n ...
- 树形DP初探•总结
这几天,我自学了基础的树形DP,在此给大家分享一下我的心得. 首先,树形DP这种题主要就是解决有明确分层次且无环的树上动态规划的题.这种题型一般(注意只是基础.普通的情况下)用深度优先搜索来解决实 ...
- 天梯赛 L1-006 连续因子 (模拟)
一个正整数N的因子中可能存在若干连续的数字.例如630可以分解为356*7,其中5.6.7就是3个连续的数字.给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列. 输入格式 ...
- php之复制文件——php经典实例
php之复制文件——php经典实例 <?php function dirCopy($dir1,$dir2){ //判断是否目录存在 if(!file_exists($dir2) || !is_d ...
- [Leetcode] N-Queens 系列
N-Queens 系列题解 题目来源: N-Queens N-Queens II N-Queens The n-queens puzzle is the problem of placing n qu ...
- Machine Learning系列--维特比算法
维特比算法(Viterbi algorithm)是在一个用途非常广的算法,本科学通信的时候已经听过这个算法,最近在看 HMM(Hidden Markov model) 的时候也看到了这个算法.于是决定 ...
- 2017 SWERC
2017 SWERC A:Cakey McCakeFace 题目描述:有一个炉每次只能放一个蛋糕,炉的进口和出口各放了一个探测器,当放蛋糕进去时,进口的探测器会记录时刻,当蛋糕做好后,蛋糕从出口出来, ...
- html---规范、细节积累-01
语义错误 块级元素可以包含内联元素和某些块级元素,内联元素不能包含块级元素,只能包含内联元素 页面可能正常解析,但不符合语义.浏览器自带容错机制,对于不规范的写法也能够正确解析,各浏览器的容错机制不同 ...