1003: Dogs and peanuts
Time Limit: 1 sec memory limit: 128 MB
Submit: 10 solved: 4
[Submit] [Status] [web board]
Description
There are n dogs. In order to train the dog's ability to play with peanuts, the master has developed three commands:
1.g I: indicates that dog I gets a peanut.
2. e I: let the dog I eat all the peanuts it gets.
3. s I j: indicates that I dog and J Dog are allowed to exchange their own peanuts.
The host has given K commands as a set of commands. To consolidate the training effect, dogs need to repeat these commands m times ......
Input
Input t to input t test data. For each test data:
Row 1st: integer n, m, K (n <= 10, m <= 10 ^ 10, k <= 100)
2nd -- k + 1 line: Indicates each command of a set of commands. Each Command is given through the (, 3) Statement in the question.
Output
For each test data, the output line contains N numbers, which indicates that after the command is repeated m times, the number of peanuts in the last hand of 1st to N dogs is separated by a space.
Sample input1
3 1 6
G 1
G 2
G 2
S 1 2
G 3
E 2 Sample output2 0 1 matrix Rapid power, construction Matrix Solution
#include<stdio.h>#include<string.h>int a[105][105],b[105][105],c[105][105],n,m;void mul(){int i,j,k;memset(b,0,sizeof(b));for(i=0;i<=n;i++)for(j=0;j<=n;j++)for(k=0;k<=n;k++)b[i][j]+=c[i][k]*a[k][j];memcpy(c,b,sizeof(b));}void mul2(){int i,j,k;memset(b,0,sizeof(b));for(i=0;i<=n;i++)for(j=0;j<=n;j++)for(k=0;k<=n;k++)b[i][j]+=a[i][k]*a[k][j];memcpy(a,b,sizeof(b));}void qm(){while(m){if(m&1) mul();mul2();m>>=1;}}int main(){int T,k,i,j,p,q,tmp;char op[5];scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&k);memset(a,0,sizeof(a));for(i=0;i<=n;i++){a[i][i]=1;c[i][i]=1;}for(i=0;i<k;i++){scanf("%s%d",op,&p);switch(op[0]){case 'g':a[n][p-1]++;break;case 'e':for(j=0;j<=n;j++) a[j][p-1]=0;break;case 's':scanf("%d",&q);for(j=0;j<=n;j++){tmp=a[j][p-1];a[j][p-1]=a[j][q-1];a[j][q-1]=tmp;}}}qm();printf("%d",c[n][0]);for(i=1;i<n;i++)printf(" %d",c[n][i]);printf("\n");}return 0;}