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) ...
随机推荐
- LintCode 156: Merge Interval
LintCode 156: Merge Interval 题目描述 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], ...
- php跳转网络连接
laravel用 redirect 跳转 HTTP 即可.可以把网址看作路由 例如: if($newsInfo->type == 77){ return redirect('http://192 ...
- Laravel 5.4 migrate时报错: Specified key was too long error
Laravel 5.4默认使用utf8mb4字符编码,而不是之前的utf8编码.因此运行php artisan migrate 会出现如下错误: [Illuminate\Database\QueryE ...
- /i,/m,/s,/x,/A,/s,/U,/x,/j,/u 等正则修饰符用法~
i (PCRE_CASELESS) 如果设置了这个修饰符,模式中的字母会进行大小写不敏感匹配. m (PCRE_MULTILINE) 默认情况下,PCRE 认为目标字符串是由单行字符组成的(然而实际上 ...
- 16级第二周寒假作业H题
快速幂(三) TimeLimit:2000MS MemoryLimit:128MB 64-bit integer IO format:%I64d Problem Description 计算( AB ...
- 【zTree】zTree展开树节点
今天在做zTree树的时候想着将第一级tree展开,于是利用下面方法: /** * 展开树节点的第一层 */ function openFirstTreenode(){ // 获取树对象 var tr ...
- 美团实习Java岗面经,已拿offer
作者:icysnowgx 链接:https://www.nowcoder.com/discuss/71954?type=2&order=3&pos=10&page=1 来源:牛 ...
- weblogic 开启注意问题
1.关闭防火墙 service iptables stop chkconfig iptables off 2.weblogic unable to get file lock问题 我的解决办法是ps ...
- wifi钓鱼 强势拿你的wifi密码
钓鱼wifi 首先设一个场景!!! 如何得到一个免费的wifi 有人可能做过抓包跑包的方法或者跑pin码的方法然而这些方法可能会耗去你大量的时间(我曾经跑包花了一天的时间 跑pin码花了一晚上)感 ...
- webconfig的配置解析
<?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...