Topic
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:
inputLine 1th: an integer n. Represents the board length. 1≤n≤100,000,000OutputLine 1th: An integer representing the number of coverage scenarios MOD 19999997
Sample input
62247088
Sample output
17748018
The first few groups write, it is easy to find the law, is a linear recursion, or even Fibonacci. So here's the problem of solving the huge Fibonacci modulus.
Tip: How to quickly calculate results
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 <iostream> #include <stdio.h> #include <mem.h>using namespace std;const int Maxn=4;const int maxm=4;const int mod=19999997;struct matrix{long long int n,m; Long long int A[MAXN][MAXM]; void Clear () {n=m=0; memset (A,0,sizeof (a)); } Matrix operator + (const matrix &b) const {matrix tmp; Tmp.n=n; Tmp.m=m; for (int i=0;i<n;++i) for (int j=0;j<m;++j) {tmp.a[i][j]= (a[i][j]+b.a[i][j])%mod; } return TMP; } Matrix operator-(const matrix &B) const {matrix tmp; Tmp.n=n; Tmp.m=m; for (int i=0;i<n;++i) for (int j=0;j<m;++j) tmp.a[i][j]= (a[i][j]-b.a[i][j])%mod; return TMP; } Matrix operator * (const matrix &B) const {matrix tmp; Tmp.clear (); Tmp.n=n; Tmp.m=m; for (int i=0;i<n;++i) {for (int j=0;j<M;++J) {for (int k=0;k<m;++k) {tmp.a[i][j]= (tmp.a[i][j ]+ (A[i][k]*b.a[k][j])%mod)%mod; }}} return tmp; }};int Solve (int a[],int b[],int n,int t) {Matrix m,f,e; M.clear (); F.clear (); E.clear (); M.n=m.m=n; E.n=e.m=n; F.n=n; F.m=1; for (int i=0;i<n-1;++i) {m.a[i][i+1]=1; } for (int i=0;i<n;i++) {m.a[n-1][i]=a[i]; F.a[i][0]=b[i]; E.a[i][i]=1; } if (T<n) return f.a[t][0]; for (t-=n-1;t;t/=2) {if (t&1) e=m*e; M=m*m; } f=e*f; return f.a[n-1][0];} int main () {int a[]={1,1}; int b[]={1,2}; int n=2; int t; cin>>t; Cout<<solve (a,b,n,t-1)%mod<<endl; return 0;}
The reason WA a gun is because the local run time with the <mem.h>,, the site of the g++ do not recognize, to use <memory.h>
Hiho_41 Week _ Domino cover One _ Recruit Law + matrix fast Power