| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 4600 | Accepted: 1051 |
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
取向量b表示最后的结果,我这里对于n个数,构造n+1维的向量,目的是有利于后面的‘g’操作
对于每一次的操作用矩阵表示,这里的矩阵a是(n+1,n+1),初始化为一个单位矩阵
‘g’ num 操作,表示使第num个数增加1,使矩阵a的第num行最后一个元素增加1,即a[num][last]+=1
‘e’ num 操作,表示使第num个数变为0,使矩阵a的第num行全部变为0
即for(i=1:i<=last;i++) a[num][i]=0;
‘s’ num1,num2操作,表示使第num1和num2元素交换位置,使矩阵a 的第num1和num2行互换就可以了for(i=1:i<=last;i++) swap(a[num1][i],a[num2][i]);
然后计算a的m次方
最后再与初始化的b向量进行一次乘法,得到的向量的1到last-1即为所要求的结果
该题注意的地方:
1:矩阵乘法中做乘法前判断两个乘数是否均为0
2:构造矩阵a的过程,开始我是每进行一次操作就用一次矩阵乘法,导致超时,其实只要每次修改下矩阵就可以了
3:注意最后结果可能超过int的范围,要用__int64
猫咪训练算法
本文介绍了一种使用矩阵运算来模拟和预测多只猫咪在执行一系列指令后的花生数量变化的算法。通过构建特定矩阵并进行幂运算,高效解决了重复指令的问题。
769

被折叠的 条评论
为什么被折叠?



