Poj 3150 cellular automaton (matrix multiplication + binary)

Source: Internet
Author: User

Question Link

The number of N is given to form a ring. One transformation is to take the remainder of the d number before and after each number, and then use it as the number. After K conversions, what is an array.

Train of Thought: here

First, let's take a look at the first group of data in the sample.
1 2 2 1 2
After a transformation
5 5 5 5 4
The principle is
A0 A1 A2 A3 A4
->
(A4 + A0 + A1) (A0 + A1 + A2) (A1 + A2 + A3) (A2 + A3 + A4) (A3 + A4 + a0)

If we use a matrix multiplication to describe it, we can express it as a matrix multiplication of 1xn and nxn, and the result is still 1xn matrix.
A = 1 2 2 1 2
B =
1 1 0 0 1
1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
1 0 0 1 1
A * B = 5 5 5 4
So the final result is: A * (B ^ K)

When the linear algebra is unqualified, the shoes indicate great pressure ..

Calculate the k power of an nxn matrix, and the K is large, and N is not small. What should I do?
As a result, some experts have observed that this matrix is somewhat special and can find some rules:
B ^ 1 =
[1, 1, 0, 0, 1]
[1, 1, 1, 0, 0]
[0, 1, 1, 1, 0]
[0, 0, 1, 1, 1]
[1, 0, 0, 1, 1]
B ^ 2 =
[3, 2, 1, 1, 2]
[2, 3, 2, 1, 1]
[1, 2, 3, 2, 1]
[1, 1, 2, 3, 2]
[2, 1, 1, 2, 3]
B ^ 3 =
[7, 6, 4, 4, 6]
[6, 7, 6, 4, 4]
[4, 6, 7, 6, 4]
[4, 4, 6, 7, 6]
[6, 4, 4, 6, 7]
B ^ 4 =
[19, 17, 14, 14, 17]
[17, 19, 17, 14, 14]
[14, 17, 19, 17, 14]
[14, 14, 17, 19, 17]
[17, 14, 14, 17, 19]

No. Is whether it is the several power of B, are in line with a [I] [J] = A [I-1] [J-1]
The experts say this is how it is pushed down:
""
Using Matrix A, B has a [I] [J] = A [I-1] [J-1], B [I] [J] = B [I-1] [J-1] (I-1 <0 represents I-1 + N, J-1 <0 represents J-1 + n)
We can conclude that the matrix C = a * B also has this property.
C [I] [J] = sum (A [I] [T] * B [T] [J]) = sum (A [I-1] [T-1], B [T-1] [J-1]) = sum (A [I-1] [T], B [T] [J-1]) = C [I-1] [J-1]
"

In this way, an array of N sizes can be opened to store the results of each calculation. Nxn is not necessary.
N has been solved, but K is still very large. What should I do?

In this case, we can use the bipartite method to obtain B ^ K.
B ^ K = B ^ 1 * B ^ 4 * B ^ 16...

1 // 3150 2 # include <cstdio> 3 # include <cstring> 4 # include <iostream> 5 # define ll long 6 7 using namespace STD; 8 9 int N, m, D, K; 10 ll a [1010], B [1010]; 11 void multi (LL * C, ll * D) 12 {13 LL X [1010]; 14 For (INT I = 0; I <n; I ++) 15 {16 x [I] = 0; 17 for (Int J = 0; j <N; j ++) 18 x [I] + = C [J] * d [I> = J? (I-j): (n + I-j)]; // prevents negative numbers, forming a ring 19} 20 for (INT I = 0; I <N; I ++) 21 d [I] = x [I] % m; 22} 23 int main () 24 {25 while (CIN> N> m> D> K) {26 for (INT I = 0; I <n; I ++) 27 Cin> A [I]; 28 B [0] = 1; 29 for (INT I = 1; I <= D; I ++) 30 B [I] = B [n-I] = 1; 31 While (k) 32 {33 If (K & 1) // odd 34 multi (B, ); 35 multi (B, B); 36 K >>= 1; 37} 38 for (INT I = 0; I <n; I ++) 39 if (I = N-1) printf ("% i64d \ n", a [I]); 40 else printf ("% i64d", a [I]); 41} 42 return 0; 43}
View code

 



In the calculation process, the number must be greater than M.
Remember x * Y = (X % m) * (Y % m)

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.