0
点赞
收藏
分享

微信扫一扫

BZOJ1026 [SCOI2009] windy数 数位DP+前导0模板

圣杰 2023-02-07 阅读 30


M - windy数

  

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

Input

  包含两个整数,A B。

Output

  一个整数

Sample Input


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


Sample Output


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


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[30];
ll dp[30][10];
ll dfs(int pos,int pre,int lead,int limit){

if(pos==-1){
if(lead)return 0;
return 1;
};
if(!limit&&!lead&&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){
if(i==0)
tmp+=dfs(pos-1,i,1,limit&&i==up);
else
tmp+=dfs(pos-1,i,0,limit&&i==up);
}
else if(abs(i-pre)>=2)
tmp+=dfs(pos-1,i,lead && i==0,limit&&i==up);
}
if(!limit&&!lead)
dp[pos][pre]=tmp;
return tmp;
}

ll solve(ll x)
{
int pos=0;
while(x)
{
a[pos++]=x%10;
x/=10;
}
return dfs(pos-1,0,true,true);
}
int main()
{

memset(dp,-1,sizeof(dp));
ll n,m;
while(~scanf("%lld%lld",&n,&m))
{
printf("%lld\n",solve(m)-solve(n-1));
}
return 0;
}

 

举报

相关推荐

0 条评论