D - Covering HDU - 6185 (杜教板子(BM)线性递推式)

本文深入探讨了D-Covering算法,一种用于解决无限地毯覆盖特定大小操场问题的高效算法。通过递推公式和模运算,算法能够在大量测试案例中快速计算出地毯覆盖方案的总数,特别适用于4xN大小的操场。文章详细介绍了算法的实现细节,包括快速幂运算、矩阵乘法优化和线性序列求解,为理解复杂数学问题提供了清晰的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D - Covering

 HDU - 6185 

Bob's school has a big playground, boys and girls always play games here after school. 

To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets. 

Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more. 

He has infinite carpets with sizes of 1×21×2 and 2×12×1, and the size of the playground is 4×n4×n. 

Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping? 

Input

There are no more than 5000 test cases. 

Each test case only contains one positive integer n in a line. 

1≤n≤10181≤n≤1018 

Output

For each test cases, output the answer mod 1000000007 in a line. 

Sample Input

1
2

Sample Output

1
5

 

//递推公式黑科技
#include<bits/stdc++.h>
using namespace std;
///#define X first
//#define Y second
#define PB push_back
//#define MP make_pair
//#define MEM(x,y) memset(x,y,sizeof(x));
//#define bug(x) cout<<"bug"<<x<<endl;
typedef long long ll;
//typedef pair<int,int> pii;
using namespace std;
//const int maxn=1e3+10;
const int mod=1000000007; //按实际改 
ll powmod(ll a,ll b){ //快速幂 
    ll res=1;a%=mod;
    assert(b>=0);
    while(b){
        if(b&1) res=res*a%mod; a=a*a%mod; b>>=1;
    }
    return res;
}
// head
namespace linear_seq {
    const int N=10010; //不需改 
    ll res[N],base[N],_c[N],_md[N];
    vector<ll> Md;
    void mul(ll *a,ll *b,int k) {
        for(int i=0;i<k+k;i++) _c[i]=0;
        for(int i=0;i<k;i++) if (a[i])
            for(int j=0;j<k;j++) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
        for (ll i=k+k-1;i>=k;i--) if (_c[i])
                for(int j=0;j<Md.size();j++)
                    _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
        for(int i=0;i<k;i++) a[i]=_c[i];
    }
    int solve(ll n,vector<ll> a,vector<ll> b) {
    // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
    //求出的是第n+1项 
        ll ans=0,pnt=0;
        ll k=a.size();
        assert(a.size()==b.size());
        for(int i=0;i<k;i++) _md[k-1-i]=-a[i];_md[k]=1;
        Md.clear();
        for(int i=0;i<k;i++) if (_md[i]!=0) Md.push_back(i);
        for(int i=0;i<k;i++) res[i]=base[i]=0;
        res[0]=1;
        while ((1ll<<pnt)<=n) pnt++;
        for (ll p=pnt;p>=0;p--) {
            mul(res,res,k);
            if ((n>>p)&1) {
                for (ll i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
                for(int j=0;j<Md.size();j++) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
            }
        }
        for(int i=0;i<k;i++) ans=(ans+res[i]*b[i])%mod;
        if (ans<0) ans+=mod;
        return ans;
    }
    vector<ll> BM(vector<ll> s) {
        vector<ll> C(1,1),B(1,1);
        int L=0,m=1,b=1;
        for(int n=0;n<s.size();n++) {
            ll d=0;
            for(int i=0;i<L+1;i++) d=(d+(ll)C[i]*s[n-i])%mod;
            if (d==0) ++m;
            else if (2*L<=n) {
                vector<ll> T=C;
                ll c=mod-d*powmod(b,mod-2)%mod;
                while (C.size()<B.size()+m) C.PB(0);
                for(int i=0;i<B.size();i++) C[i+m]=(C[i+m]+c*B[i])%mod;
                L=n+1-L; B=T; b=d; m=1;
            } else {
                ll c=mod-d*powmod(b,mod-2)%mod;
                while (C.size()<B.size()+m) C.PB(0);
                for(int i=0;i<B.size();i++) C[i+m]=(C[i+m]+c*B[i])%mod;
                ++m;
            }
        }
        return C;
    }
    int gao(vector<ll> a,ll n) {
        vector<ll> c=BM(a);
        c.erase(c.begin());
        for(int i=0;i<c.size();i++) c[i]=(mod-c[i])%mod;
        return solve(n,c,vector<ll>(a.begin(),a.begin()+c.size()));
    }
};
 //用的时候只用改mod的值和前几项的数值 
int main(){
    ll n;
    while(~scanf ("%lld", &n)){ //求第n项 //一般放入前8项 
         printf("%lld\n",linear_seq::gao(vector<ll>{1,5,11,36,95,281,781,2245},n-1));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值