HDU 6172 and HDU 6185 【线性递推 + 思维 + 板子】

这两道题都是给的线性递推式(输入只有一个未知数n), 那么我写这个博客的目的就是保存一个超强模板, 可以解决任何线性递推式. 这个板子是我从百度之星复赛上”偷”的杜教的板子. 所以我们现在要做的是用绝对正确的方法求出递推式的前几项. 然后扔进这个板子就可以了.

//这些板子都可以直接用, 不用管mod, 带了mod的. 所以用的时候只用改mod的值和前几项的数值. 当然前几项丢的越多越好, 一般8个是绝对可以推出来的. 个别的少一点也行.
HDU 6172
//题意不多说.
AC Code

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((ll)(x).size())
typedef vector<ll> VI;
typedef pair<ll,ll> PII;
const ll mod = 1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

ll _,n;
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,ll k) {
        rep(i,0,k+k) _c[i]=0;
        rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
        for (ll i=k+k-1;i>=k;i--) if (_c[i])
            rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
        rep(i,0,k) a[i]=_c[i];
    }
    ll solve(ll n,VI a,VI b) {
        // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
//        printf("%d\n",SZ(b));
        ll ans=0,pnt=0;
        ll k=SZ(a);
        assert(SZ(a)==SZ(b));
        rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
        Md.clear();
        rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
        rep(i,0,k) 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;
                rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
            }
        }
        rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
        if (ans<0) ans+=mod;
        return ans;
    }
    VI BM(VI s) {
        VI C(1,1),B(1,1);
        ll L=0,m=1,b=1;
        rep(n,0,SZ(s)) {
            ll d=0;
            rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
            if (d==0) ++m;
            else if (2*L<=n) {
                VI T=C;
                ll c=mod-d*powmod(b,mod-2)%mod;
                while (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) 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 (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
                ++m;
            }
        }
        return C;
    }
    ll gao(VI a,ll n) {
        VI c=BM(a);
        c.erase(c.begin());
        rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
        return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
    }
};

int main()
{
    for (scanf("%lld",&_);_;_--){
        scanf("%lld",&n);
        printf("%lld\n",linear_seq::gao(VI{31,197,1255,7997},n-2));
    }
}

HDU 6185
//题意不多说.
AC Code

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((ll)(x).size())
typedef vector<ll> VI;
typedef pair<ll,ll> PII;
const ll mod = 1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

ll _,n;
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,ll k) {
        rep(i,0,k+k) _c[i]=0;
        rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
        for (ll i=k+k-1;i>=k;i--) if (_c[i])
            rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
        rep(i,0,k) a[i]=_c[i];
    }
    ll solve(ll n,VI a,VI b) {
        // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
//        printf("%d\n",SZ(b));
        ll ans=0,pnt=0;
        ll k=SZ(a);
        assert(SZ(a)==SZ(b));
        rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
        Md.clear();
        rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
        rep(i,0,k) 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;
                rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
            }
        }
        rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
        if (ans<0) ans+=mod;
        return ans;
    }
    VI BM(VI s) {
        VI C(1,1),B(1,1);
        ll L=0,m=1,b=1;
        rep(n,0,SZ(s)) {
            ll d=0;
            rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
            if (d==0) ++m;
            else if (2*L<=n) {
                VI T=C;
                ll c=mod-d*powmod(b,mod-2)%mod;
                while (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) 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 (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
                ++m;
            }
        }
        return C;
    }
    ll gao(VI a,ll n) {
        VI c=BM(a);
        c.erase(c.begin());
        rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
        return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
    }
};

int main() {
    while(~scanf("%lld",&n)){
        printf("%lld\n",linear_seq::gao(VI{1,5,11,36,95,281,781,2245},n-1));
    }
}

//对比一下这两个板子也可以知道就是改的那部分. 还有注意的是具体传多少项, 和后面那个传n的什么样子, 都是试出来的, 要自己感觉对了, 即把后面的几项打出来看. 那么才有可能对. 所以还是要试出来的.
//需要声明的一点是这个序列前几项怎么推出. 第一个例题前面几项还是比较好推的, 根据题目所给的式子.
那么后面一个例题, 前面几项怎么推的还是有一定难度的(其实知道前面几项了, 也是可以找到递推式子的, 当然用矩阵快速幂做也是可以的, 但是明显这个板子是很强的呀(矩阵快速幂写起来还是非常复杂的)!!! 但注意这个板子只能适用于任何的线性递推式子, 其他的递推式不一定适用), 所以正解就是用二进制枚举或者是dfs深搜出前面几项. 然后套一套杜教的板子就可以了. (或者是自己根据这几项找递推式子)

附上打表找前几项的代码: 只要能准确的找出来, 再暴力的方法都是允许的.
dfs版本: (也是比较容易想到的, 注意细节处理就行)

/** @Cain*/
int n,cnt;
int mapp[7][15];

bool ok(int &x,int &y)
{
    for(int i=1;i<=4;i++){
        for(int j=1;j<=n;j++){
            if(!mapp[i][j]){
                x = i;
                y = j;
                return false;
            }
        }
    }
    return true;
}

void dfs(int x,int y)
{
    if(!mapp[x+1][y] && x+1<=4){
        mapp[x][y] = mapp[x+1][y] = 1;   // 纵向的填.
        int tmpx,tmpy;
        if(ok(tmpx,tmpy)){
            mapp[x][y] = mapp[x+1][y] = 0; //注意填满的时候的处理.
            cnt++;
            return ;
        }
        dfs(tmpx,tmpy);   // 从没有填满的地方继续搜索.
        mapp[x][y] = mapp[x+1][y] = 0;  //回溯的时候注意处理.
    }
    if(!mapp[x][y+1] && y+1<=n){  
        mapp[x][y] = mapp[x][y+1] = 1;   // 横向的填.模仿着上面的来做.
        int tmpx,tmpy;
        if(ok(tmpx,tmpy)){
            mapp[x][y] = mapp[x][y+1] = 0;
            cnt++;
            return ;
        }
        dfs(tmpx,tmpy);
        mapp[x][y] = mapp[x][y+1] = 0;  
    }
}

int main()
{
    for(int i=1;i<=10;i++){
        n = i;
        cnt = 0;
        dfs(1,1);
        printf("%d%c",cnt,i==10?'\n':',');
    }
} //然后直接丢进杜教的板子就可以了.xx

二进制枚举版本: (这个想法是非常重要的, 很多题都可以用这样打出表来) 跑前几项还是要跑一会的. 但跑出来了就是赢了. 这个版本相对于dfs的肯定是要慢很多的. 但是在有些情况下是非常有用的.

/** @Cain*/
typedef long long int ll ;
int mapp[10][20];
bool ok(int n, int m) {
    int vis[10][20] = {0};   //表示当前这个格子是否用过. 保证只用一次.
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (vis[i][j] == 0) {
                int f = 0;
                if (mapp[i][j] == 0) {
                    int tx = i;
                    int ty = j + 1;
                    if (ty <= m && vis[tx][ty] == 0 && mapp[tx][ty] == mapp[i][j]) {
                        f = 1;
                        vis[tx][ty] = 1;
                        vis[i][j] = 1;
                    }
                }
                else {
                    int tx = i + 1;
                    int ty = j;
                    if (tx <= n && vis[tx][ty] == 0 && mapp[tx][ty] == mapp[i][j]) {
                        f = 1;
                        vis[tx][ty] = 1;
                        vis[i][j] = 1;
                    }
                }
                if (!f) return false;
            }
        }
    }
    return true;
}

void solve() {
    for (int i = 1; i <= 10; i++) {   //i 是列数.
        int tot = i * 4;
        ll num = 0;
        for (ll st = 0; st < (1ll<<tot); st++) {   //将这些所有的格子看成二进制的一位.
            ll cur = st;                       //通过二进制枚举就可以一个不漏的枚举出来.
            for (int x = 1; x <= 4; x++) {
                for (int y = 1; y <= i; y++) {
                    mapp[x][y] = cur % 2;       //用当前这个数的二进制去填这些框框.
                    cur /= 2;
                }
            }
            num += ok(4, i);
        }
        printf("%d%c",num,i==10?'\n':',');
    }
}

// 所以对于二进制枚举的, 请多想想!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值