F. ant decoration

A single point of time: 2.0 sec

Memory Limit: 512 MB

A month to school, and learn to love the little ant want to celebrate! So it put its "home" to decorate.
First thing to do is paste the floor.
Little Ant "home" ground can be viewed as a 2 * N  squares, it has numerous pieces 1 * 2  and  2 * 1 floor.
Please help calculate the ants under a total of how many ways can give ground filled.
The floor can not be cut, nor overlap.

Input Format

A single set of inputs:
there is a number  N 
where  ( 0 < N < 10 18 is )

Output Format

Outputs discharge method 10 . 9 + 7 results modulo

Sample

input
2
output
2
input
1
output
1 
This topic is ,, there is a pit board 1 * 2 * 1 and 2, the same kind of wooden planks
and then hit the table to find the law f (1) = 1; f (2) = 2, f (3) = 3 , f (4) = 5 ... similarly to the Fibonacci columns

AC codes:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
struct stu{
    ll arr[3][3];
};
this mul (x this, and this) {
    this years;
    memset(ans.arr,0,sizeof(ans.arr));
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                ans.arr [i] [j] = (ans.arr [i] [j]% versus + x.arr [i] [k] * y.arr [k] [j]% v)% v;
    return ans;
}
stu ksm(stu x,ll y){
    this res;
    memset(res.arr,0,sizeof(res.arr));
    for(int i=0;i<2;i++)
        res.arr[i][i]=1;
    while(y){
        if(y&1) res=mul(res,x);
        x=mul(x,x);
        y>>=1;    
    }    
    return res;    
}

int main () {
    ll n;
    cin>>n;
    this arr1, arr2;
    arr1.arr [ 0 ] [ 0 ] = 1 ;
    arr1.arr [ 0 ] [ 1 ] = 1 ;
    arr1.arr [ 1 ] [ 0 ] = 1 ;
    arr1.arr [ 1 ] [ 1 ] = 0 ;
    memset(arr2.arr,0,sizeof(arr2.arr)); 
    arr2.arr[0][0]=2;
    arr2.arr[1][0]=1;
    this years;
    if(n==2){
        cout<<2<<endl;
    }
    else if(n==1) {
        cout<<1<<endl;
    }
    else{ 
        years = ksm (OFF1, n 2 );
        years = mul (years arr2);
        cout<<ans.arr[0][0]%mod<<endl; 
    }
    return 0;
}

 






Guess you like

Origin www.cnblogs.com/Accepting/p/11297245.html
ANT
ANT