Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12261 | Accepted: 3016 |
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只猫,给你以下三种操作:
1.g x,给第x只猫增加一份猫食
2.e x,第x只猫吃掉当前自己所有的猫食
3.s x y,交换第x只猫和第y只猫的猫食
如此我们构造矩阵如下:
初始化矩阵A : [1 0 0 0 0...]
针对单位矩阵B:
1.g x,直接对B[0][x] ++
2.e x,对于B[i][x] = 0,
3.s x y,对于swap(B[i][x], B[i][y])
然后针对这样构成的矩阵进行快速幂处理
输出A * B ^ m的结果即可
/*头文件模板*/ #include <map> #include <set> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <vector> #include <cctype> #include <cstdio> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <iomanip> #include <typeinfo> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define pb push_back #define mp make_pair #define mem(a, x) memset(a, x, sizeof(a)) #define copy(a, b) memcpy(a, b, sizeof(a)) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define FIN freopen("input.txt", "r", stdin) #define FOUT freopen("output.txt", "w", stdout) typedef long long LL; typedef pair<int, int > PII; typedef pair<int, string> PIS; typedef pair<LL, LL>PLL; typedef unsigned long long uLL; template<typename T> void print (T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void print (const T &a, string bes = "") { int len = bes.length(); if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } template<typename T> void debug (T* p, T* q, string Gap = " ", bool flag = false) { #ifndef ONLINE_JUDGE int d = p < q ? 1 : -1; cout << "Debug out : "; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; #endif } template<typename T> void debug (const T &a, string bes = "") { #ifndef ONLINE_JUDGE int len = bes.length(); cout << "Debug out : "; if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; #endif } void IO_Init() { ios::sync_with_stdio (false); } LL LLabs (const LL a) { return a >= 0 ? a : -a; } const double PI = 3.141592653589793; const double eps = 1e-10; const int MAXM = 1e5 + 5; const int MAXN = 1e5 + 5; const int INF = 0x3f3f3f3f; /*头文件模板*/ typedef vector<LL> vec; typedef vector<vec> mat; mat mul(mat &A, mat &B) { mat C(A.size(), vec(B[0].size())); for(int i = 0; i < A.size(); i ++) { for(int k = 0; k < B.size(); k ++) { if(!A[i][k]) continue; for(int j = 0; j < B[0].size(); j ++) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]); } } } return C; } mat pow(mat A, LL n) { mat B(A.size(), vec(A[0].size())); for(int i = 0; i < A.size(); i ++) { B[i][i] = 1; } while(n) { if(n & 1) B = mul(B, A); A = mul(A, A); n >>= 1; } return B; } int n, m, k; int main() { #ifndef ONLINE_JUDGE //FIN; //FOUT; #endif IO_Init(); char op[2]; int x, y; while(~scanf("%d%d%d", &n, &m, &k), n || m || k) { mat A(n + 1, vec(n + 1)),B(1, vec(n + 1)); for(int i = 0;i <= n;i ++) A[i][i] = 1; B[0][0] = 1; for(int j = 0; j < k; j ++) { scanf("%s", op); if(op[0] == 'g') { scanf("%d", &x); A[0][x] ++; } else if(op[0] == 'e') { scanf("%d", &x); for(int i = 0; i <= n; i ++) { A[i][x] = 0; } } else { scanf("%d%d", &x, &y); for(int i = 0; i <= n; i ++) { swap(A[i][x], A[i][y]); } } } A = pow(A, m); B = mul(B, A); for(int i = 1; i <= n; i ++) { printf("%lld%c", B[0][i], i == n ? '\n' : ' '); } } return 0; }