poj 3735

来源:互联网 发布:淘宝上索尼z3c屏幕总成 编辑:程序博客网 时间:2024/06/11 01:40
G - Training little cats
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3735

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, wheren 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

题意,有N只小喵,另其做一系列操作,g i :令 i 拿一只坚果,s i j :把 i 和 j 的坚果交换,e i :令 i 把所拥有的坚果都吃掉。

把这一系列操作重复M次,问最后每只小喵各拥有多少只坚果;

思路:因为M很大,可以联想到快速幂,所以就把这一系列的操作转化为对矩阵的操作,先初始化为单位矩阵B, g i:B[0][i]++,s i j:把第 i 列和第 j 列交换,e i 令第 i 列全为0;

matrix mult(matrix a,matrix b){ ///矩阵相乘   matrix ans;   ans.n=a.n;ans.m=b.m;   for(int i=0;i<=ans.n;i++){      for(int j=0;j<=ans.m;j++){         ans.dat[i][j]=0; ///一定要有,新定义的矩阵不一定全为0         for(int k=0;k<=ans.m;k++){            ans.dat[i][j]+=a.dat[i][k]*b.dat[k][j];         }      }   }   return ans;}

这种会超时,由于操作对象为稀疏矩阵,加个判断就好了

matrix mult(matrix a,matrix b){ ///矩阵相乘   matrix ans;   ans.n=a.n;ans.m=b.m;   for(int i=0;i<=ans.n;i++){      for(int j=0;j<=ans.m;j++){         ans.dat[i][j]=0; ///一定要有,新定义的矩阵不一定全为0         for(int k=0;k<=ans.m;k++){            if(a.dat[i][k] && b.dat[k][j]) ///不加就超时 ,然而还是觉得复杂度差不了多少啊T_T               ans.dat[i][j]+=a.dat[i][k]*b.dat[k][j];         }      }   }   return ans;}

AC代码

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>#include<queue>using namespace std;typedef long long ll;int N,M,K;char c;struct matrix{ ///定义矩阵   int n,m;   ll dat[105][105];};void pri_ma(matrix a){ ///打印矩阵   printf("n=%d m=%d\n",a.n,a.m);   for(int  i=0;i<=a.n;i++){      for(int j=0;j<=a.m;j++){         printf("%d ",a.dat[i][j]);      }      printf("\n");   }   printf("\n");}matrix init(matrix a){ ///初始化为单位矩阵   a.n=N;   a.m=N;   for(int i=0;i<=N;i++){      for(int j=0;j<=N;j++){        a.dat[i][j]=0;      }      a.dat[i][i]=1;   }   return a;}matrix mult(matrix a,matrix b){ ///矩阵相乘   matrix ans;   ans.n=a.n;ans.m=b.m;   for(int i=0;i<=ans.n;i++){      for(int j=0;j<=ans.m;j++){         ans.dat[i][j]=0; ///一定要有,新定义的矩阵不一定全为0         for(int k=0;k<=ans.m;k++){            if(a.dat[i][k] && b.dat[k][j]) ///不加就超时               ans.dat[i][j]+=a.dat[i][k]*b.dat[k][j];         }      }   }   return ans;}matrix mi(matrix a,int x){ ///矩阵快速幂    matrix res;    res=init(res);    while(x){       if(x&1) res=mult(res,a);       a=mult(a,a);       x>>=1;    }    return res;}int main(){  // freopen("G.txt","r",stdin);   while(scanf("%d%d%d",&N,&M,&K) && (N || M || K) ) {       matrix A,B;       B=init(B);       for(int i=0;i<K;i++){          getchar();          scanf("%c",&c);          if(c=='g'){             int a;             scanf("%d",&a);             B.dat[0][a]++;          }          if(c=='s'){             int a,b,t;             scanf("%d%d",&a,&b);             for(int j=0;j<=N;j++){                 t=B.dat[j][a];                 B.dat[j][a]=B.dat[j][b];                 B.dat[j][b]=t;             }          }          if(c=='e'){             int a;scanf("%d",&a);             for(int j=0;j<=N;j++){                B.dat[j][a]=0;             }          }       }       B=mi(B,M);///做矩阵B的M次幂       for(int i=1;i<=N;i++)         printf("%lld ",B.dat[0][i]);       printf("\n");   }   return 0;}





0 0
原创粉丝点击