Hihocoder 1143: Domino cover problem • One (recursive, Matrix fast Power)

Source: Internet
Author: User

"Topic link": Click here~~

time limit:10000ms single point time limit:1000ms memory limit:256MB
Description

Dominoes, an ancient toy. Today we are going to look at the problem of Domino coverage:
We have a 2xN strip board and then use the 1x2 dominoes to cover the entire board. How many different coverage methods are there for this chessboard?
For example, for a board with a length of 1 to 3, we have the following types of coverage:

Hint: Domino Overlay

Tip: How to quickly calculate results

input

Line 1th: an integer n. Represents the board length. 1≤n≤100,000,000

Output

Line 1th: An integer representing the number of coverage scenarios MOD 19999997

Sample input
62247088
Sample output
17748018
fast power of "thought" Matrix

We consider how to place the new Domino (blue) in the case where a partial domino (gray) has been placed:

The right side of the situation is impossible, otherwise there will always be more than one lattice no way to place dominoes. Or the gray portion of the lattice is an odd number, it can not be placed through a 1x2 dominoes.
so by looking at the above, we can find:
at the end of any placement scenario, the first two scenarios must be met. The gray part corresponds to the placement scheme of the length N-1 and N-2. Thus, we can get a recursive formula:
F[n] = f[n-1] + f[n-2];
does this formula look familiar? That's right, that's our Fepolaci series.
f[0]=1,f[1]=1,f[2]=2,...

When n is very small, we can calculate it directly by the recursive formula. When n is very large, as long as our computer is good enough, we can still calculate it directly from the recursive formula.
But we learn the algorithm, always such a direct enumeration is not very low, so we have to use a good algorithm to speed up (loaded x).
In fact, for this linear recursion, we can use matrix multiplication to find the nth term. For the Fibonacci sequence of the subject, we want to find a 2x2 matrix M that makes (a, b) x M = (b, a+b), where (A, b) and (b, a+b) are all 1x2 matrices.
Obviously, just take m = [0, 1; 1, 1] on it:

further get:

So the next question is, can you quickly calculate the m^n? Let's start by analyzing the power operation. Since the multiplication is to satisfy the binding law, we have:

you may wish to k[1]. K[J] A better division?

where (k[1],k[2]...k[j]) 2 means that n is represented as a number for each digit after the binary number. The above formula satisfies such a property:

combining the two we can get an algorithm:
1. First calculate all {a^1, a^2, a^4 ... a^ (2^j)}, because the sequence satisfies the recursive formula, the time complexity is O (logn)
2. The exponent n binary, and then use the formula to multiply the corresponding a^j to calculate the a^n, the time complexity is still O (logn)
The total time complexity is O (logn)
this algorithm, because it can find a power in a short time, we call the "fast power" algorithm.

Code:

 #include <bits/stdc++.h>using namespace std; #define LL Long Longconst ll mod=19999997; ll N;int i,j;struct matrlc{ll mapp[2][2];} ans,base; MATRLC unit= {1,0,0,1};    MATRLC mult (MATRLC A,MATRLC B)//matrix multiplication {MATRLC C;            for (int i=0, i<2; i++) for (int j=0; j<2; J + +) {c.mapp[i][j]=0;            for (int k=0; k<2; k++) c.mapp[i][j]+= (a.mapp[i][k]*b.mapp[k][j])%mod;        C.mapp[i][j]%=mod; } return C;}    ll Pow (ll N)//fast power operation {Base.mapp[0][0] =base.mapp[0][1]=base.mapp[1][0]=1;    base.mapp[1][1]=0;    Ans.mapp[0][0] = ans.mapp[1][1] = 1;//ans initialized to the unit matrix ans.mapp[0][1] = ans.mapp[1][0] = 0;        while (n) {if (n&1) Ans=mult (ans,base);        Base=mult (base,base);    n>>=1; } return ans.mapp[0][1]%mod;}   int main () {scanf ("%lld", &n);   printf ("%lld\n", pow (n+1)%mod); return 0;} 

/* Title: First, the topic is a Fibonacci series. Let's look at how the third figure is made up of the first two graphs.    ______           _______|   | |  or | |____||         ____|_|           |__|____| expands to nth graph, we have: _____________ ______________|  | |         or | |____||         ___________|_| |_________|____| So, f (n) =f (n-1) +f (n-2) because n can be very large, so we need some computational techniques. The Fibonacci sequence can be computed from the matrix, as follows: [a,b]* [0,1] = [B,A+B] [x] [[]] [[]] [[]] so, theoretically, we multiply n matrix mat, we can get f (n), but n matrices multiply, time complexity is O (n), at this time, we use the fast power operation to solve, can be reduced to the complexity of O (Logn). */#include <string> #include <iomanip> #include <fstream> #include <set> #include <queue> #include <map>//#include <unordered_set>//#include <unordered_map>//#include <sstream>//# Include "func.h"//#include <list> #include <stdio.h> #include <iostream> #include <string># include<memory.h> #include <limits.h>//#include <stack> #include <vector> #include <  algorithm>using namespace std; #define MOD 19999997class Matrix22{public:long Long A1, A2;long longB1, B2;matrix22 (): A1 (0), A2 (1), B1 (1), B2 (1) {};matrix22 operator* (const matrix22 TMP)//overloaded matrix multiplication {matrix22 mat;mat.a1 = (a1%mod) * (tmp.a1%mod) + (a2%mod) * (tmp.b1%mod); mat.a2 = (a1%mod) * (tmp.a2%mod) + (a2%mod) * (tmp.b2%mod); mat.b1 = (B1%MOD) * (Tmp.a1%mod) + (b2%mod) * (tmp.b1%mod); mat.b2 = (b1%mod) * (tmp.a2%mod) + (b2%mod) * (TMP.B2%MOD); return mat;}};/  * Function Name: main function function: primary function */int Main (void) {int n;scanf ("%d", &n), int dp1 = 1;int DP2 = 2;if (n <= 0) printf ("0\n"); else if (n = = 1) printf ("1\n"), else if (n = = 2) printf ("2\n"), else{n-= 3;matrix22 mat;matrix22 ans;while (n! = 0) {///If binary that bit is 1, a Ns*matif (n & 1) ans = ans*mat;//mat each time it is multiplied by itself, the 1,2,4,8,16 of the matrix is obtained, the square mat = Mat*mat;n = (n >> 1);} Output f (n) long long answer = (ans.a2 + 2 * ans.b2)%mod;cout << answer << Endl;} return 0;}

Hihocoder 1143: Domino cover problem • One (recursive, 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.