BZOJ 1026 windy数

传送门

题解:二维dp,第一维记录位置,第二维记录前一个。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include<ctime>
//#include<unordered_map>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0)
const int N=100;
const ull base=163;
const int INF=0x3f3f3f3f;
using namespace std;
int a[N];
int dp[20][10];
int dfs(int pos,int pre,int lead,int limit){
    if(pos==-1)return 1;
    if(!lead&&!limit&&dp[pos][pre]!=-1)return dp[pos][pre];
    int up=limit?a[pos]:9;
    int tmp=0;
    for(int i=0;i<=up;i++){
        if(lead){
            tmp+=dfs(pos-1,i,lead&&i==0,limit&&i==a[pos]);
        }
        else{
            if(abs(pre-i)>=2)
                tmp+=dfs(pos-1,i,lead&&i==0,limit&&a[pos]==i);
        }
    }
    if(!lead&&!limit)dp[pos][pre]=tmp;
    return tmp;
}
int solve(int x){
    int pos=0;
    while(x){
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true,true);
}
int main(){
    int a,b;
    cin>>a>>b;
    memset(dp,-1,sizeof(dp));
    printf("%d\n",solve(b)-solve(a-1));
    return 0;
}

 

posted @ 2018-05-27 21:59  采蘑菇的小西佬  阅读(153)  评论(0编辑  收藏  举报