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) ...
随机推荐
- 你不知道的JavaScript--数组进阶全掌握
转载 http://blog.csdn.net/i10630226/article/details/49764537 在程序语言中数组的重要性不言而喻,JavaScript中数组也是最常使用的对象之一 ...
- IO流--切割 合并文件
import java.io.*; import java.util.*; public class io { public static void main(String[] args)throws ...
- Java 集合系列 01 总体框架
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- SAP连接HANA数据库
既然都用HANA了,为什么还要在SAP端,连接HANA数据库,做数据库处理..... 因为HANA数据库,没个用户在STADIO上建的数据库表...只能这个用户使用,而做Universe 设计的时候, ...
- 修改Linux系统时区
修改配置文件来修改时区1.修改/etc/sysconfig/clock ZONE=Asia/Shanghai 2.rm /etc/localtime3.链接到上海时区文件 ln -sf ...
- JDicom使用指南
适用条件本指南用于使用JDicom进行环境模拟.产品调试. 一.安装JDicom运行JDicom安装程序之前,需安装JRE 1.3及以上版本.否则,弹出如下图所示报错 安装JRE 1.4:双击运行可执 ...
- 闲聊CSS之关于clearfix--清除浮动[转]
.clearfix:after { content: " "; display: block; clear: both; height:; } .clearfix { zoom:; ...
- 学习记录008-crond和visudo
1.每隔两个小时将/etc/servers文件打包备份到.tmp下(每次名字不同) [root@kaka cc.log]# tar zcvf /server/backup/ccc.log_$(date ...
- OLAP vs OLTP: what makes the difference
OLAP vs OLTP: what makes the difference OLPT and OLAP are complementingtechnologies. You can't live ...
- NSDate的运算
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...