Matrix classic question 4: gifts for Christmas Eve (using M replicas to transform the sequence)

Source: Internet
Author: User

Https://vijos.org/p/1049


Returns a sequence Containing N numbers. Then there are m replicas, K replicas for the initial sequence, and the final sequence.


Let's first look at a replacement, represent the replacement as a matrix, and then multiply the M replicas. Then, the initial sequence first executes this replacement K/m times, then sequentially executes the first K % m replacement, and then multiply the initial matrix.

Finally, pay attention to the order of Matrix Multiplication. A * B! = B *.


# Include <stdio. h> # include <iostream> # include <map> # include <set> # include <list> # include <stack> # include <vector> # include <math. h> # include <string. h> # include <queue> # include <string> # include <stdlib. h> # include <algorithm> # define ll long # DEFINE _ ll _ int64 # define EPS 1e-12 # define PI ACOs (-1.0) # Define c 240 # define s 20 using namespace STD; const int maxn = 110; struct matrix {int mat [maxn] [maxn]; I NT n, m; void Init () {memset (MAT, 0, sizeof (MAT); For (INT I = 1; I <= 105; I ++) {mat [I] [I] = 1 ;}} M [11]; // represents each replacement. Int n, m; matrix MUL (matrix A, matrix B) {matrix ans; memset (ans. mat, 0, sizeof (ans. mat); ans. N =. n; ans. M = B. m; For (INT I = 1; I <=. n; I ++) {for (int K = 1; k <=. m; k ++) {if (. mat [I] [k] = 0) continue; For (Int J = 1; j <= B. m; j ++) ans. mat [I] [J] + =. mat [I] [k] * B. mat [k] [J] ;}} return ans;} matrix POW (matrix A, int N) {matrix ans; ans. N =. n; ans. M =. m; ans. init (); While (n) {If (N & 1) ans = Mul (ANS, a); A = MUL (a, a); N >=1;} return ans;} int main () {int num, K, Z; while (~ Scanf ("% d", & N, & M, & K) {matrix A, B;. init ();. M =. N = N; B. N = N; B. M = 1; for (INT I = 1; I <= N; I ++) B. mat [I] [1] = I; for (INT I = 1; I <= m; I ++) {memset (M [I]. mat, 0, sizeof (M [I]. mat); m [I]. M = m [I]. N = N; For (Int J = 1; j <= N; j ++) {scanf ("% d", & num); m [I]. mat [J] [num] = 1;} A = MUL (M [I], a); // note the order of multiplication} z = K/m; k = K % m; A = POW (A, Z); For (INT I = 1; I <= K; I ++) A = MUL (M [I], a); // note the order of multiplication A = MUL (a, B); For (INT I = 1; I <= N; I ++) {printf ("% d",. mat [I] [1]); if (I = N) printf ("\ n"); else printf ("") ;}} 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.