1026: [SCOI2009]windy数(数位dp)

1026: [SCOI2009]windy数

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 10739  Solved: 5051
[Submit][Status][Discuss]

Description

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

Input

  包含两个整数,A B。

Output

  一个整数

Sample Input

【输入样例一】
1 10
【输入样例二】
25 50

Sample Output

【输出样例一】
9
【输出样例二】
20

HINT

 

【数据规模和约定】

100%的数据,满足 1 <= A <= B <= 2000000000 。

解 :因为有是否为前导零的区别 比如 0001是可以的 但是 0和 1的差值在2以内 ,所以我们把如果为前导零 ,那么我们pre传一个20回去

#include <stdio.h>
#include <algorithm>
#include<string.h>
using namespace std;
int a[20];//拆分出的数字
int dp[20][30];//结果
int dfs(int pos,int pre ,bool limit)//位数   前一位(如果前面都是零 那么pre=20)  限制
{
    if(pos==-1) return 1;
    if(!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(abs(i-pre)<2) continue;//超过2 
        if(pre==20&&i==0)
             tmp+=dfs(pos-1,20,limit&&i==a[pos]);
        else
            tmp+=dfs(pos-1,i,limit&&i==a[pos]);
    }
      return limit ? tmp : dp[pos][pre] = tmp;
}
int slove (int x)
{
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x=x/10;
    }
    return dfs(pos-1,20,true);
}
int main()
{
   int a,b;
   while(~scanf("%d%d",&a,&b))
   {
       memset(dp,-1,sizeof(dp));
       printf("%d\n",slove(b)-slove(a-1));
   }
}
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值