POJ 3735 矩阵快速幂

来源:互联网 发布:软件销售管理制度 编辑:程序博客网 时间:2024/05/29 08:46
Training little cats
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14555 Accepted: 3602

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 nm 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

PKU Campus 2009 (POJ Monthly Contest – 2009.05.17), Facer


#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;}


/*几个注意
稀疏矩阵可以通过颠倒kjl顺序来预判0, 从而加速!
矩阵多出一维,用来做加减操作
原创粉丝点击