https://vijos.org/p/1049
P1049 presents for Christmas EveAccepted Tags: combinatorial math gifts for Christmas Eve [show tags] Return code interface | Close Pascal
- Pascal
- C
- C++
- Python 2.x
- Java
Cancel | Empty code Description
When the elves have written their cards. The Elves of the Gift preparation department have made all the gifts. But because of mental consumption, the quality of their gifts is getting smaller, which means that Santa Claus is not satisfied. But this is no way to do things.
So Santa Claus gift preparation Department of the elves gathered up, explaining their own views: "Now you have n gifts, the quality is descending order." So in order to keep this gift sequence on average, not as regular descending as it is now, I have a list here. ”
"The list has m rows, which are called operations (not sequences), each row has n numbers, and the numbers are different and each number is between 1 and N. At first, the gift sequence is the present place of the gift, that is, the first gift sequence is 1, 2, 3, 4......N; Then, we look at the first line of the list operation, set the number of the line operation is A[i], then the original sequence of A[i] A gift is placed in the current position of this sequence, and then a new gift sequence is formed. Then, look at the second row of the list ..., the third line of action ... Repeat the above operation until the last line. When the last line is finished, the sequence is followed by the first line, and then the second line is manipulated ... Third line operation ... Until a total of K-lines have been operated. The final generation of this sequence is our final gift to the children of the sequence. Did you get it? ”
"Got it!" ”
When Santa Claus smiled away, everyone began to get busy. Because the M values can be large and large, the elves operate at a limited speed. So it may not be possible to finish the task before Santa Claus goes to deliver the gift. Make them very annoyed ...
Format input Format
The first line is three numbers, n,m and K.
Next m line, n number per row.
Output format
A row, a total of n number, represents the final gift sequence. n the number is separated by a space, there is no space at the end of the line, need to enter.
Example 1 sample input 1[copy]
7 5 86 1 3 7 5 2 43 2 4 5 6 7 17 1 3 4 5 2 65 6 7 3 1 2 42 7 3 4 6 1 5
Sample output 1[Copy]
2 4 6 3 5 1 7
Test instructions: The initial sequence is 1.2.3...N, and then gives M operations, each operation has n number, indicating that the number of the first a[i] is placed on the position of the first, the topic to ask after the K-operation of the sequence
Analysis: After the sequence.m<=10, k<2^31.
First put thisM permutationThe merge"Get up (figure out thisM permutations), and then we need to perform this permutationK/m Times (rounding, if the remainder of the remaining few steps to simulate can be). Note that any permutation can be expressed in the form of a matrix. For example, you would1 2 3 4 replacement for3 1 2 4, equivalent to the following matrix multiplication:
(line I, A[i] is 1, because the number of a[i] is to be placed in the first place)
Permutation k/m Times is the equivalent of multiplying the k/m by a matrix such as the preceding one. We can calculate the k/m of the matrix by two points , then multiply the initial sequence. Don't be too busy to be happy, when you are dead, don't forget that there may be several permutations that need to be simulated at the end.
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5 using namespacestd;6 structMat7 {8 intmat[ the][ the];9 };TenMat p[ the]; One intn,m,k; AMatoperator*(Mat X, Mat y) - { - Mat C; thememset (C.mat,0,sizeof(C.mat)); - for(intt =1; t <= N; t++) - { - for(inti =1; I <= N; i++) + { - for(intj =1; J <= N; J + +) +C.MAT[I][J] + = x.mat[i][t] *Y.mat[t][j]; A } at } - returnC; - } -Matoperator^ (Mat x,intt) - { - Mat C; in for(inti =1; I <= N; i++) - for(intj =1; J <= N; J + +) toC.MAT[I][J] = (i = =j); + while(t) - { the if(T &1) *c = c *x; $x = x *x;Panax NotoginsengT >>=1; - } the returnC; + } A intMain () the { + while(SCANF ("%d%d%d", &n,&m,&k)! =EOF) - { $ Mat res,c,a; $ for(inti =1; I <= N; i++) -a.mat[i][1] =i; - the for(inti =1; I <= N; i++) - for(intj =1; J <= N; J + +)WuyiRES.MAT[I][J] = (i = =j); the - for(inti =1; I <= m; i++) Wu { -memset (C.mat,0,sizeof(C.mat)); About for(intj =1; J <= N; J + +) $ { -scanf"%d", &p[i].mat[j][1]); -c.mat[j][p[i].mat[j][1] ] =1; - } Ares = c *Res; + } the -res = res ^ (k/m); $A = res *A; the inttemp = k%m; the for(inti =1; I <= temp; i++) the { thememset (C.mat,0,sizeof(C.mat)); - for(intj =1; J <= N; J + +) inc.mat[j][p[i].mat[j][1] ] =1; theA = c *A; the } About for(inti =1; I < n; i++) the { theprintf"%d", a.mat[i][1]); the } +printf"%d\n", a.mat[n][1]); - the }Bayi return 0; the}
View Code
P1049 Gifts for Christmas Eve (matrix ten issues four)