BZOJ 2660 Beijing wc2012 最多的方案

本题直接暴力+剪枝即可。

用一个map记忆化每种情况的DP值,然后用一个前缀和优化就能秒A辣!

实测最坏情形下的状态数仅有162 !!!!!

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll n;
vector<ll> all;
ll s[100];
map<pair<ll , ll> , ll> d;

ll dp(ll dep , ll v)
{
    if(v > s[dep]) return 0;
    if(dep < 0) return v == 0;
    if(d.count(make_pair(dep , v))) return d[make_pair(dep , v)];

    ll res = dp(dep-1 , v);
    if(v >= all[dep]) res += dp(dep-1 , v-all[dep]);
    return d[make_pair(dep , v)] = res;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
    #endif

    cin>>n;
    all.push_back(1); all.push_back(2);
    while(true)
    {
        ll now = all.back() + all[all.size()-2];
        if(now <= n) all.push_back(now);
        else break;
    }

    s[0] = 1;
    for(unsigned int i=1;i<all.size();i++) s[i] = s[i-1] + all[i];
    cout<<dp(all.size()-1 , n)<<endl;
    return 0;
}

你可能感兴趣的:(暴力)