BZOJ 1026: [SCOI2009]windy数

简单的数位dp

直接用dfs的模板写

坑点:0001是合法的,但是之前没考虑前导0导致将其判为不合法的

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
int bit[50];
ll f[50][55];

ll dfs(int pos,int last, bool flag)
{
    if(pos == 0) return 1;
    if(flag && f[pos][last] != -1) return f[pos][last];
    ll ans = 0;
    int up = flag ? 9 : bit[pos];
    for(int i = 0; i <= up; i++)
    {
        if(abs(i - last) < 2) continue;
        else{
            if(last == 50 && i == 0)
                ans += dfs(pos-1, 50, flag || i < up);
            else
                ans += dfs(pos-1, i, flag || i < up);
        }
    }
    if(flag) f[pos][last] = ans;
    return ans;
}
ll solve(ll x)
{
    int len = 0;
    while(x){
        bit[++len] = x % 10;
        x /= 10;
    }
    return dfs(len,50,false);
}
int main()
{
    int t;
    memset(f,-1,sizeof(f));
    ll a;scanf("%lld",&a);
    ll b;scanf("%lld",&b);
    printf("%lld\n",solve(b) - solve(a-1));
    return 0;
}
View Code

 

posted on 2019-03-18 19:19  solvit  阅读(143)  评论(0编辑  收藏  举报

导航