Vijos1049 gift matrix multiplication for Christmas Eve

Source: Internet
Author: User
/* Question: m replicas are given sequentially, and the M replicas are used repeatedly to operate the initial sequence, asking the sequence after K replicas. M <= 10, k <2 ^ 31. First, merge the M replicas (calculate the product of the M replicas), and then we need to execute the replicas K/M (take the integer, if there is an remainder, you can simulate it in a few steps ). Note that any replacement can be expressed as a matrix. For example, replacing 1 2 3 4 with 3 1 2 4 is equivalent to the following matrix multiplication: [[0 0 1 0] [1] [3] [1 0 0 0] [2] = [1] [0 1 0 0] * [3] [2] [0 0 0 1] [4] [4] replacing K/M is equivalent to multiplying K/m in front of this matrix. we can calculate the k/M power of the matrix in binary mode and multiply it by the initial sequence. Don't be too busy to be happy. The time of your satisfaction is the day of your destruction. Don't forget that there may be several replacements that need to be simulated in the end. */

 
 
Description

When the Genie have all the cards written. The genie from the gift Preparation Department have made all the gifts. However, because of their mental consumption, the quality of their gifts is getting smaller and smaller, that is, they are getting more and more satisfied with Santa Claus. But there is no way to do this.

So Santa Claus gathered the genie from the gift Preparation Department and explained his opinion: "Now you have n gifts, whose quality is sorted in descending order. So in order to keep the gift sequence average, unlike the regular descending order, I have a list here ."
"The list contains m rows, which are called operations (not sequences). Each row has n numbers. These numbers are different and each number is between 1 and N. At the beginning, the sequence of gifts is the current position of the present gift. That is to say, the sequence of gifts at the beginning is 1, 2, 3, 4 ...... N; then, let's look at the first row of the list and set the I Number of this row to a [I], then, place the first [I] gift in the original sequence to the position of the second in the current sequence, and then form a new gift sequence. Then, view the second operation of the list ...... And the third line ...... Repeat the previous operation until the last row. When the operation on the last row ends, the sequence is followed by the first line, and then the second line is operated ...... The third operation ...... Keep repeating until K rows are operated in total. The final generation of this sequence is the sequence of the final gifts we give to the children. Do you understand ?"
"Understand !"
After Santa Claus smiled, Everyone began to get busy. Because the m value may be very large, and the operation speed of the Elves is limited. Therefore, Santa Claus may not be able to complete this task before giving gifts. They are very angry ......

Format input format

The first row has three numbers: n, m, and K.

Next, the number of N rows in m rows.

Output Format

A row with N numbers indicates the final gift sequence. N numbers are separated by a space. There is no space at the end of the line. Press enter.

Example 1 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
Restrictions

1 s for each test point

Prompt

1 <= n <= 100; 1 <= m <= 10; 1 <= k <= 2 ^ 31-1.

K <= 50% for 500 of data. These data points are 8 points, while other data points are 12 points.

 

# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <algorithm> # include <cstdlib> # define maxn 110 using namespace STD; struct matrix {int size; long element [maxn] [maxn]; void setsize (INT); void setmodulo (INT); Matrix Operator * (matrix ); matrix power (INT) ;}; void matrix: setsize (int A) {for (INT I = 0; I <A; I ++) for (Int J = 0; j <A; j ++) element [I] [J] = 0; size = A;} matrix: Opera Tor * (matrix PARAM) {matrix product; product. setsize (size); For (INT I = 0; I <size; I ++) for (Int J = 0; j <size; j ++) for (int K = 0; k <size; k ++) product. element [I] [J] + = element [I] [k] * Param. element [k] [J]; return product;} matrix: Power (INT exp) {matrix res, A; A = * This; Res. setsize (size); For (INT I = 0; I <size; I ++) res. element [I] [I] = 1; while (exp) {If (exp & 1) RES = res * A; exp> = 1; A = A * ;} return res;} mat Rix ans, A, data [110]; int N, K, M; long squ [110], tsqu [110]; int main () {While (~ Scanf ("% d", & N, & M, & K) {ans. setsize (n); For (INT I = 0; I <n; I ++) ans. element [I] [I] = 1, SQU [I] = I + 1; for (INT I = 0; I <m; I ++) {. setsize (n); For (Int J = 0; j <n; j ++) {long TMP; scanf ("% lD", & TMP);. element [J] [tmp-1] = 1;} data [I] = A; ans = A * ans;} int mod, div; mod = K % m; DIV = K/m; ans = ans. power (DIV); For (INT I = 0; I <n; I ++) {long TMP = 0; For (Int J = 0; j <N; j ++) TMP + = (ans. element [I] [J] * squ [J]); tsqu [I] = TMP;} memcpy (SQU, Tsqu, sizeof (tsqu); For (INT Mm = 0; mm <MOD; mm ++) {memcpy (SQU, tsqu, sizeof (tsqu )); for (INT I = 0; I <n; I ++) {long TMP = 0; For (Int J = 0; j <n; j ++) TMP + = data [mm]. element [I] [J] * squ [J]; tsqu [I] = TMP ;}} for (INT I = 0; I <n; I ++) printf ("% LD % s", tsqu [I], I! = N-1? "": "\ N");} return 0 ;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.