bzoj1026 [SCOI2009]windy数(数位dp)

题目链接:点这里!!!


题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数?


题解:数位dp。设dp[i][j]为第i位时第i+1位为j时符合条件的数有多少个?注意一下前导零的情况和各个位有没有填满的问题就可以了。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
const LL  MOD = 1000000007;
const int N = 10+15;
const int maxn = 8e3+15;
const int letter = 130;
const int INF = 2e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int x,y,k;
int q[N][N],dp[N][N],b[N];
int dfs(int bo,int i,int j){
    if(i==0&&j!=10&&j!=-2) return 1;
    if(i==0) return 0;
    if(bo&&q[i][j]) return dp[i][j];
    if(bo){
        q[i][j]=1;
        int &pc=dp[i][j];
        if(j==10){
            pc+=dfs(bo,i-1,10);
            for(int h=1;h<10;h++) pc+=dfs(bo,i-1,h);
        }
        else {
            for(int h=0;h<10;h++)
                if(abs(h-j)>1)    pc+=dfs(bo,i-1,h);
        }
        return pc;
    }
    else {
        int pc=0;
        for(int h=0;h<=b[i];h++){
            if(i==k&&h==0)  pc+=dfs(1,i-1,10);
            else if(h==b[i]&&abs(h-j)>1) pc+=dfs(0,i-1,h);
            else if(h!=b[i]&&abs(h-j)>1) pc+=dfs(1,i-1,h);
        }
        return pc;
    }
}
int get(int x){
    k=0;
    while(x){b[++k]=x%10,x/=10;}
    return dfs(0,k,-2);
}
int main(){
    scanf("%d%d",&x,&y);
    printf("%d\n",get(y)-get(x-1));
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值