10.04 T4 equation + matrix fast Power

Source: Internet
Author: User

The Castle Road

(Road.pas/c/cpp)

Title Description

BB to go to the castle of SS to play. We can think of two castles on the same axis, BB's castle coordinates are 0,SS's castle coordinates are n. Under normal circumstances, BB will go to the SS castle in the same direction (i.e., the SS Castle in the direction of the BB Castle), and the step size is 1 or 2. However, today BB on the way met the kitten from SPB, under the surprise, incredibly have a step to go the opposite direction! However, BB is not delirious, it is only one step in the opposite direction, and this step is 1 or 2 steps. At the same time, BB will not pass through the SS castle and keep down. Of course, BB is on the way to meet KK, so it will not be in their own door in the wrong direction.
For example, if the castle coordinates of the SS are 3, then the following two paths are valid:
0->1->2->1->3
0->1->-1->1->3
Of course, there are other legal paths. The following paths are not legal:
0->-1->1->3 (BB can't go the wrong way in the first step)
0->1->3 (BB must be a step in the wrong direction)
0->2->1->0->2->3 (BB Only one step is going in the wrong direction)
0->-1->0->3 (the length of the BB must be 1 or 2 per step)
0->1->2->4->3 (BB will not go over the SS castle and come back again)
0, 1, 2, 3, 2, 3 (BB will stop once it reaches the castle of SS)
You need help now BB to find out how many ways it can reach the SS castle?

Input format

A line of an integer n that represents the coordinates of the SS castle.

Output format

An integer line that represents the number of different paths of BB to SS Castle. Since this number can be very large, you only need to output its mod 1000000007 results.

Sample input

2

Sample output

5

Sample Description

For the example, the following 5 paths are legal:
0->1->0->2
0->1->-1->0->1->2
0->1->-1->0->2
0->1->0->1->2
0->1->-1->1->2
Data scope and conventions

For 10% of data, n<=20.
For 70% of data, n<=1000.
For 90% of data, n<=1000000.
For 100% of data, n<=10^15.

Algorithm One

Direct violence search, search how to walk each step, while judging the legality of the program.
Complexity of Time: O (n!)
Space complexity: O (N)
Expected score: 10 points

Algorithm two

We notice that we can enumerate the following two things:
1, BB Where to return a step
2, BB Returns the PACE is 1 or 2
After determining these two things, we can easily calculate the total number of scenarios that satisfy both conditions by the Fibonacci series = Sigma (f[x] * f[n-x+b])
Where F represents the Fibonacci sequence, f[0]=f[1]=1;x represents the return time coordinates of our enumerated BB, B represents the step size of the BB we enumerate, and n represents the coordinates of the SS castle.
Complexity of Time: O (N2)
Space complexity: O (N)
Expected score: 70 points

Algorithm three

We found that the time of the algorithm two was largely wasted on the enumeration. Is there a way to return the position without enumerating it?
The answer is yes. We can put the SS castle coordinates at 1. 5 o'clock Answer ans[n] expand to see:
Ans[1] = 0 + 0 + 0 + 0
ANS[2] = f[1] * f[2] + f[1] * f[3] = f[1] * (f[2] + f[3]) = f[1] * f[4] + 0 + 0 + 0
ANS[3] = f[1] * f[3] + f[1] * F[4] + f[2] * f[2] + f[2] * f[3] = f[1] * f[4] +0 + f[4] * f[2] + f[3] * F[1]
ANS[4] = f[1] * F[4] + f[1] * f[5] + f[2] * f[3] + f[2] * F[4] + f[3] * f[2] +f[3] * f[3] = f[1] * f[4] + f[4] * f[2] + f[3] * f[1]+ f[1] * f[4] + f[4]* f[3] + f[3]* f[2]

ANS[5] = f[1] * f[5] + f[1] * F[6] + f[2] * f[4] * f[2] *f[5] + f[3 [f[3] + f[3] * F[4] + f[4] * f[2] + f[4] * F[3] = F[1] * F[4] + f[4] * f[2] + f[3] * f[1] + f[1] * f[4] + f[4]* f[3] + f[3]* f[2] + f[1] * F[4] + f[4] *f[2] + f[3] * F[1] + f[4] * F[4] + f[3] * F[3]
It can be noted that the above transformations clearly represent the recursive formula of Ans[i].
We still use f[i] to represent the Fibonacci sequence. wherein, the red Word denotes ans[i-1], the yellow word denotes ans[i-2], the blue Word denotes f[4]*f[i-1], the green word denotes f[3]*f[i-2].
∴f[i] =f[i-1] + f[i-2]
Ans[i] = Ans[i-1] + ans[i-2] + f[4] * F[i-1] + f[3] * F[i-2]
Time complexity: O (N)
Space complexity: O (N)
Expected score: 90 points

Algorithm Four

Note that the maximum number of n can be 1015, so you need to use matrix multiplication to add the express push.
By the algorithm three, we have defined the recursive type.
So, just construct the matrix a ={
1 1 5 3
1 0 0 0
0 0 1 1
0 0 1 0}
Among them, the transverse four numbers are respectively represented Ans[i-1],ans[i-2], f[i-1], f[i-2]
Portrait four numbers respectively Ans[i],ans[i-1], F[i], f[i-1]
Then we use the N-2 power of this matrix to multiply the vector b ={
5
0
2
1}
These four numbers represent ans[2],ans[1], f[2], f[1].
After multiplying to get a matrix of 4 * 1, the answer is the number in the first column of the first row.
When calculating the N-2 power of matrix A, we can use matrix multiplication to quickly power the time complexity to O (Logn).
Complexity of Time: O (LOGN)
Space complexity: O (LOGN)
Expected score: 100 points

10.04 T4 equation + matrix fast Power

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.