Training little cats_矩阵快速幂
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花生,g x表示给第x只猫咪一颗花生,e x表示第x只猫咪把花生全吃了,s x y表示交换x和y 猫咪的花生数;
将上述k次操作进行m次,求最后每只猫咪的花生数。
【思路】由于m的数非常大,所以一般的方法肯定会Tel,所以用矩阵快速幂
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<cstdlib>
#include<map>
using namespace std;
const int N=;
long long int n,m,k;
struct Mat
{
long long int mat[N][N];
void clear()
{
memset(mat,,sizeof(mat));
}
void init()
{
clear();
for(int i=;i<=n;i++)
{
mat[i][i]=;
}
}
};
Mat a;
Mat mul(Mat a,Mat b)//矩阵乘法
{
Mat c;
c.clear();//刚开始把c.init()改了好久
for(int i=;i<=n;i++)
{
for(int k=;k<=n;k++)
{
if(a.mat[i][k])
{
for(int j=;j<=n;j++)
{
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
}
}
}
}
return c;
}
Mat pow(Mat a,long long int m)//快速幂
{
if(m==) return a;
Mat c;
c.init();
while(m)
{
if(m&) c=mul(c,a);
m>>=;
a=mul(a,a);
}
return c; } int main()
{
while(~scanf("%lld%lld%lld",&n,&m,&k))
{
if(!n&& !m&& !k) break;
a.init();
while(k--)
{
long long int x,y;
char op[];
scanf("%s",op);
if(op[]=='g')
{
scanf("%lld",&x);
a.mat[][x]++;
}
else if(op[]=='e')
{
scanf("%lld",&x);
for(int i=;i<=n;i++)
{
a.mat[i][x]=;
}
}
else
{
scanf("%lld%lld",&x,&y);
for(int i=;i<=n;i++)
{
swap(a.mat[i][x],a.mat[i][y]);
}
}
}
if(m==)
{
printf("");
for(int i=;i<=n;i++)
{
printf("");
}
printf("\n");
continue;
} a=pow(a,m);
printf("%lld",a.mat[][]);
for(int i=;i<=n;i++)
{
printf(" %lld",a.mat[][i]);
}
printf("\n"); }
return ;
}
Training little cats_矩阵快速幂的更多相关文章
- poj 3753 Training little cats_矩阵快速幂
题意: 通过各种操作进行,给第i只猫花生,第i只猫吃光花生,第i只猫和第j只猫互换花生,问n次循环操作后结果是什么 很明显是构建个矩阵,然后矩阵相乘就好了 #include <iostream& ...
- 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<矩阵快速幂/稀疏矩阵的优化>
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13488 Accepted: ...
- hdu4686 Arc of Dream 2013 Multi-University Training Contest 9矩阵快速幂
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Tot ...
- POJ 3735 Training little cats 矩阵快速幂
http://poj.org/problem?id=3735 给定一串操作,要这个操作连续执行m次后,最后剩下的值. 记矩阵T为一次操作后的值,那么T^m就是执行m次的值了.(其实这个还不太理解,但是 ...
- 矩阵快速幂 POJ 3735 Training little cats
题目传送门 /* 题意:k次操作,g:i猫+1, e:i猫eat,s:swap 矩阵快速幂:写个转置矩阵,将k次操作写在第0行,定义A = {1,0, 0, 0...}除了第一个外其他是猫的初始值 自 ...
- Training little cats(poj3735,矩阵快速幂)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10737 Accepted: ...
- 2014 Super Training #10 G Nostop --矩阵快速幂
原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...
- poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
随机推荐
- if条件语句练习题
习题一: 做一个算缘分的小游戏:输入男方姓名,女方姓名,输出缘分指数,给出建议. static void Main(string[] args) { //做一个算缘分的小游戏: //输入男方姓名,女方 ...
- NodeJS无所不能:细数10个令人惊讶的NodeJS开源项目
在几年的时间里,NodeJS逐渐发展成一个成熟的开发平台,吸引了许多开发者.有许多大型高流量网站都采用NodeJS进行开发,像PayPal,此外,开发人员还可以使用它来开发一些快速移动Web框架. 除 ...
- 转:Linux 安装 Mysql
前段时间安装了Mysql,但是有些问题,就想把他卸载了,重新安装一个,但是没想到在Linux卸载软件是一个很痛苦的事情. 我的Mysql是用命令的方式安装的,就是上一篇文章用到的那个命令(sudo ...
- [Js]碰撞运动
描述:撞到目标点弹回来(速度反转) 一.无重力的漂浮div var div1=document.getElementById("div1"); var iSpeedX=6; var ...
- bzoj 1823: [JSOI2010]满汉全席
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ],next[ ...
- 关于http断点续传相关的RANGE这个header
<?php //1.txt内容"1234567890" socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=0-0\r\ ...
- php 获取当前url,可以规避框架url重写后还有index.php的情况
function get_url(){ $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL ...
- [开发笔记]-FireWorks常用操作快捷键
一.工具快捷键 指针.选择后方对象[V],[0] 部分选定[A],[1] 选取框.椭圆选取框[M] 套索.多边形套索[L] 裁剪.导出区域[C] 魔术棒[W] 线条工具[N] 钢笔工具[P] 矩形.圆 ...
- linux下格式化硬盘与挂载硬盘
格式化: mkfs -t ext4 /dev/sdb 自动挂载: 编辑/etc/fstab文件 sudo nano /etc/fstab,如下图将设备/dev/sdb硬盘挂载到/home/solr/s ...
- 如何利用SVN合并代码
一. 背景 平时在进行开发时,一般都会有多版本同时进行,包括项目版本.周版本.紧急版本等,当某一个版本具备上线条件后,需要在上一个已发布的版本基础上进行发布,才能够避免出现版本相互覆盖,因此 ...