HDUOJ 6185 Covering

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185

题意:1*22*1的骨牌要完全铺满4*n的地板,问有多少种方案。

思路:暴力打了个表,然后发现了如下递推式:

f(n)=f(n-1)+5*f(n-2)+f(n-3)-f(n-4)(n\geq 5)

矩阵快速幂一下就好了,注意取模。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
template<typename T> inline T gcd(T&a,T&b){return b==0?a:gcd(b,a%b);}
template<typename T> inline T lcm(T&a,T&b){return a/gcd(a,b)*b;}
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
ll n;
const int G=4;
const ll P=1000000007LL;
struct matrix{
    #define MS(x,y) memset(x,y,sizeof(x))
    #define MC(x,y) memcpy(x,y,sizeof(x))
    ll v[G][G];
    void O(){MS(v,0);}
    void E(){MS(v,0);for(int i = 0; i < G; ++i)v[i][i] = 1; }
    void print(){
        for (int i=0;i<G;i++){
            for (int j=0;j<G;j++){
                printf("%d ",v[i][j]);
            }
            puts("");
        }
    }
    matrix operator*(const matrix &b)const{
        matrix c;c.O();
        for (int k=0;k<G;k++){
            for (int i=0;i<G;i++)if(v[i][k]){
                for (int j=0;j<G;j++){
                    c.v[i][j]=(c.v[i][j]+(ll)v[i][k]*b.v[k][j])%P;
                    c.v[i][j]=(c.v[i][j]+P)%P;
                }
            }
        }
        return c;
    }
    matrix operator+(const matrix &b)const{
        matrix c;c.O();
        for (int i=0;i<G;i++){
            for (int j=0;j<G;j++){
                c.v[i][j]=(v[i][j]+b.v[i][j])%P;
                c.v[i][j]=(c.v[i][j]+P)%P;
            }
        }
        return c;
    }
    matrix operator^(ll p)const{
        matrix y; y.E();
        matrix x; memcpy(x.v,v,sizeof(v));
        while (p){
            if (p&1) y=y*x;
            x=x*x;
            p>>=1;
        }
        return y;
    }
}A,B;
void init(){
    A.O(),B.O();
    A.v[0][0]=A.v[0][2]=A.v[1][0]=A.v[2][1]=A.v[3][2]=1;
    A.v[0][3]=-1;A.v[0][1]=5;
    B.v[0][0]=1;B.v[2][0]=1,B.v[3][0]=1;
}
int main(){
    while (~scanf("%I64d",&n)){
        init();
        A=A^n;
        A=A*B;
        printf("%I64d\n",A.v[0][0]);
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注