Matrix (Magic Algorithm)

Source: Internet
Author: User

Last night, the seniors taught us such a magic algorithm---matrix fast power, matrix fast power in recursive optimization is quite magical, and high efficiency.

 First, give such an example. We all know about the Fibonacci sequence. F[N]=F[N-1]+F[N+2] (n=108), seeking f[n];

This kind of topic, if use recursion to do go down certainly time-out. But it's easy to solve with matrices.

F[N] * 0 1 = f[n+1]

F[n+1] ... A 1 1 ... C f[n+2] ... b a matrix *c matrix gets B matrix. The C matrix is introduced, and the main core is the introduction of a matrix with a to get the B matrix. (That is, I know the current state and the next state, I will ask the intermediary medium, so that can be infinitely aware of the next state, down the state ....)  The value of ~ ~) we know f[1]=1,f[2]=1; A*cn=b, and then to the Cn quick power to take the mold is OK!

After a little bit of understanding, change it.

 Two, there are a,b,c,d four cities, as shown . from the a->d in the condition of just n steps to go. N is very big and big. How many ways to ask.

The initial of this is to walk 0 steps to the next position of the Matrix a=e (the unit matrix), and then the obvious mediation matrix is a B c D

A 0 1 1 0

b 0 0 1 1

C 1 1 0 1

D 0 1 1 0....C then a*c can get the number of two steps to a place. Where "1" indicates that A->b has a method.

Third, there are still a,b,c,d four cities,. I'm going to go from a to D in exactly n steps, plus one requirement is the shortest distance. To find the shortest distance. Similar to the previous, this is not a detailed explanation 0.0

Seniors also talked about the regional game several topics, 1:30 will say not clear, from the morning summary to now, should go to eat = = Summarize it

Matrix Fast Power Main is to write the three matrix to solve the problem, although now is only a thought, which is doped with some dynamic programming problems, but very practical, can be very fast to solve the N value is particularly large, or similar combinatorial problems.

Matrix (Magic Algorithm)

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.