您的位置:首页 > 产品设计 > UI/UE

UESTC 250 windy数(数位DP)

2015-09-02 08:53 871 查看
Description

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

Input

包含两个整数A和B满足 1 <=A <= B <=2000000000

Output

输出区间[A,B]中的windy数个数

Sample Input

1 10

Sample Output

9

Solution

数位DP,用dp[len][pre]表示当前位为len位,前一位为pre时windy数的个数

Code

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
ll dp[12][12];
int num[12];
ll dfs(int len,bool fir,int pre,bool fp)
{
if(!len)
return fir==0;
if(!fp&&dp[len][pre]!=-1&&!fir)
return dp[len][pre];
ll ret=0;
int fpmax=fp?num[len]:9;
for(int i=0;i<=fpmax;i++)
if(fir||i-pre>=2||pre-i>=2)
ret+=dfs(len-1,fir&&i==0,i,fp&&i==fpmax);
if(!fir&&!fp)
dp[len][pre]=ret;
return ret;
}
ll f(ll n)
{
int len=0;
while(n)
{
num[++len]=n%10;
n/=10;
}
return dfs(len,1,11,1);
}
int main()
{
ll a,b;
memset(dp,-1,sizeof(dp));
while(~scanf("%lld%lld",&a,&b))
printf("%lld\n",f(b)-f(a-1));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: