POJ 3735 矩阵快速幂
来源:互联网 发布:软件销售管理制度 编辑:程序博客网 时间:2024/05/29 08:46
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 6g 1g 2g 2s 1 2g 3e 20 0 0
Sample Output
2 0 1
Source
#include<cstdio>#include<string.h>#include<algorithm>#include<cstring> long long matx[105][105];long long ansmatx[105][105];long long temp[105][105];long long n, m, k1, x, y;char c;using namespace std;void matrix_init() {memset(matx, 0, sizeof(matx));memset(ansmatx, 0, sizeof(matx));for (int i = 0; i <= n; i++)matx[i][i] = 1, ansmatx[i][i] = 1;}void matrix_add(int x) {matx[x][0]++;}void matrix_swap(int x, int y) {for (int i = 0; i <= n; i++)swap(matx[x][i], matx[y][i]);}void matrix_delete(int x) {for (int i = 0; i <= n; i++)matx[x][i] = 0;}void matrix_pow() {for (int i = m; i>0; i >>= 1) {if (i & 1) {memset(temp, 0, sizeof(temp));for (int k = 0; k <= n; k++)for (int l = 0; l <= n; l++)if (matx[k][l])for (int j = 0; j <= n; j++)temp[k][j] += ansmatx[l][j] * matx[k][l];memcpy(ansmatx, temp, sizeof(temp));}/*for (int i = 0; i <= n; i++)for (int j = 0; j <= n; j++)ansmatx[i][j] = temp[i][j];*/memset(temp, 0, sizeof(temp));for (int k = 0; k <= n; k++)for (int l = 0; l <= n; l++)if (matx[k][l])for (int j = 0; j <= n; j++)temp[k][j] += matx[l][j] * matx[k][l];/*for (int i = 0; i <= n; i++)for (int j = 0; j <= n; j++)matx[i][j] = temp[i][j];*/memcpy(matx, temp, sizeof(temp));}}int main() {scanf("%d%d%d", &n, &m, &k1);while (n) {matrix_init();for (int i = 0; i<k1; i++) {scanf("%s", &c);if (c == 'g') {scanf("%d", &x);matrix_add(x);}else if (c == 's') {scanf("%d%d", &x, &y);matrix_swap(x, y);}else {scanf("%d", &x);matrix_delete(x);}}matrix_pow();for (int i = 1; i <= n; i++)printf("%lld ", ansmatx[i][0]);printf("\n");scanf("%d%d%d", &n, &m, &k1);}return 0;}
- POJ 3735 矩阵快速幂
- POJ 3233 矩阵快速幂
- POJ 3150 矩阵快速幂
- POJ 3070 矩阵快速幂
- POJ - Fibonacci 【快速幂 + 矩阵】
- poj 3070 矩阵快速幂
- poj 3233 矩阵快速幂
- poj 3233 矩阵快速幂
- poj 3070(矩阵快速幂)
- poj 3070 矩阵快速幂
- 矩阵快速幂 poj 3070
- POJ 3070 矩阵快速幂
- poj 3233(矩阵快速幂)
- POJ 3070 矩阵快速幂
- poj 3233 矩阵快速幂
- POJ 3233 矩阵快速幂
- poj 3233(矩阵快速幂)
- POJ 3070 矩阵快速幂
- Core ML学习
- C
- 无线Mesh网络简介
- Git常用命令
- 汉诺塔问题(递归)
- POJ 3735 矩阵快速幂
- hdu 6154 CaoHaha's staff(思维题的意思就是做不出来的纯粹是脑子不行)
- Hdu 6144 Arithmetic of Bomb 模拟
- "mysql"不是内部或外部命令,也不是可运行的程序 处理方法
- KMP字符串匹配算法
- 最少拦截系统 下降子序列的个数
- 第十七天
- 继承和派生
- Scala入门学习之【类,对象,继承】