Fibonacci sequence F (n) "N super-Large (matrix accelerated operation) template"

Source: Internet
Author: User
Tags modulus mul

Hihocoder #1143: Domino Coverage problem • Time limit:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe

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 

Analysis: n oversize, if the Fibonacci nth term is calculated recursively, the matrix of linear algebra has the effect of accelerating operation.

But some of the sequences may be: 1 1 2 3, because a little bit different requires a slight modification of the number of times the matrix is multiplicative, which is the exponent of the matrix.
Code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include < string> #include <vector> #include <queue> #include <math.h> #define EPS 1e-8#include <algorithm >using namespace std;//Matrix fast power operation (matrix acceleration operation) struct matrix{long long a[2][2];//define 2x2 matrix};matrix mul (Matrix x, Matrix Y, L Ong MoD) {matrix ret;//The value of each element of the RET matrix is multiplied by the matrix and then returned to it ret.a[0][0]= ((x.a[0][0]%mod) *y.a[0][0]%mod + (X.a[0][1]%mod) *y.a[    1][0]%mod)%mod;    Ret.a[0][1]= ((x.a[0][0]%mod) *y.a[0][1]%mod + (x.a[0][1]%mod) *y.a[1][1]%mod)%mod;    Ret.a[1][0]= ((x.a[1][0]%mod) *y.a[0][0]%mod + (x.a[1][1]%mod) *y.a[1][0]%mod)%mod;    Ret.a[1][1]= ((x.a[1][0]%mod) *y.a[0][1]%mod + (x.a[1][1]%mod) *y.a[1][1]%mod)%mod; return ret;}    To find the power modulus of Matrix X, E is the exponential matrix Mypow (Matrix X, Long long e, long MoD)//(X^E)%mod{matrix ret, temp;        if (e==0) {ret.a[0][0]=1; ret.a[0][1]=0; ret.a[1][0]=0;        Ret.a[1][1]=1;    return ret; } if (e==1) return x; When the index is 1 o'clock, return to the originalMatrix Temp=mypow (x, e>>1, MoD); x E/2 Ret=mul (temp, temp, mod); Ret=temp*temp if (e&1) Ret=mul (ret, x, MoD); If E is odd, ret times x return ret;    Return answer}int Main () {long n, m=19999997;//m is mod matrix ans;        while (scanf ("%ld", &n)!=eof) {//matrix initialization ans.a[0][0]=1; ans.a[0][1]=1; Ans.a[1][0]=1;        ans.a[1][1]=0;        if (n) {ans = Mypow (ans, N, m);//n Here is the exponent (considering whether n is to be modified in the corresponding topic), M is take modulus printf ("%lld\n", ans.a[0][0]);        }else{printf ("0\n"); }} return 0;}

Fibonacci sequence F (n) "N super-Large (matrix accelerated operation) template"

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.