UESTC250 windy数 (相邻两数之差不小于2) 数位dp

题意:http://acm.uestc.edu.cn/#/problem/show/250

windy定义了一种windy数。

不含前导零且相邻两个数字之差至少为2 的正整数被称为windy数。

windy想知道,在A 和B 之间,包括A 和B ,总共有多少个windy数?

Input

包含两个整数,B 。

满足 1AB20000000001≤A≤B≤2000000000 .

OutputSample Input

1 10

Sample Output

9
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<math.h>
#include<string>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define N 106
#define Lson rood<<1
#define Rson rood<<1|1
LL dp[40][12][3],d[60];
LL dfs(int now,int up,int falg,int fp)
{
    if(now==1) return (!falg)||(falg&&!up);
    if(!fp&&dp[now][up][falg]!=-1) return dp[now][up][falg];
    LL ans=0;
    int ma=fp?d[now-1]:9;
    for(int i=0;i<=ma;i++)
    {
        if(!falg&&abs(i-up)<2) continue;
        ans+=dfs(now-1,i,falg&&i==0&&now-1!=1,fp&&i==ma);
    }
    if(!fp&&dp[now][up][falg]==-1) dp[now][up][falg]=ans;
    return ans;
}
LL calc(LL x)
{
    if(x==0) return 1;
    LL xxx=x;
    int len=0;
    while(xxx)     
    {
        d[++len]=xxx%10;
        xxx/=10;
    }  
    LL sum=0;
    for(int i=0;i<=d[len];i++)
        sum+=dfs(len,i,i==0,i==d[len]);
    return sum;
}
int main()
{
    LL l,r;
    memset(dp,-1,sizeof(dp));
    while(scanf("%lld%lld",&l,&r)!=EOF)
        printf("%lld\n",calc(r)-calc(l-1));
    return 0;
}

 

转载于:https://www.cnblogs.com/a719525932/p/7770572.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值