JZOJ 1039. 【SCOI2009】windy数

题目

Description
windy定义了一种windy数。
不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。
windy想知道,在A和B之间,包括A和B,总共有多少个windy数?
 
Input
两个整数,A B。
Output
一个整数,表示A~B中有多少个windy数。
 
Sample Input
1 10
Sample Output
9
 
Data Constraint
 
 
Hint
100%的数据,满足 1 <= A <= B <= 2000000000 。

 

分析

 

  • 数位DP板子题

 

代码

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 using namespace std;
 5 int e[10001],cnt;
 6 long long f[12][12][2][2];
 7 long long dp(int pos,int pre,bool zero,bool lim)
 8 {
 9     if (pos<1) return 1;
10     if (f[pos][pre][zero][lim]!=-1) return f[pos][pre][zero][lim];
11     int end=lim?e[pos]:9;
12     int ans=0;
13     for (int i=0;i<=end;i++)
14     {
15         if (abs(i-pre)>=2||zero)
16         {
17             ans+=dp(pos-1,i,zero&&(i==0),lim&&(i==end));
18         }
19     }
20     return f[pos][pre][zero][lim]=ans;
21 }
22 long long calc(int x)
23 {
24     memset(f,-1,sizeof(f));
25     memset(e,0,sizeof(e));
26     cnt=0;
27     while (x) e[++cnt]=x%10,x/=10;
28     return dp(cnt,0,1,1);
29 }
30 int main ()
31 {
32     int a,b;
33     cin>>a>>b;
34     cout<<calc(b)-calc(a-1);
35 } 

 

转载于:https://www.cnblogs.com/zjzjzj/p/11330411.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值