1026: [SCOI2009]windy数
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7939 Solved: 3576
[ Submit][ Status][ Discuss]
Description
windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,
在A和B之间,包括A和B,总共有多少个windy数?
Input
包含两个整数,A B。
Output
一个整数
Sample Input
【输入样例一】
1 10
【输入样例二】
25 50
1 10
【输入样例二】
25 50
Sample Output
【输出样例一】
9
【输出样例二】
20
9
【输出样例二】
20
HINT
【数据规模和约定】
100%的数据,满足 1 <= A <= B <= 2000000000 。
数位DP的详解看大神博客:http://blog.csdn.net/wust_zzwh/article/details/52100392
#include<stdio.h>
#include<string.h>
#include<math.h>
int a[15];
int dp[15][15];
int Dfs(int pos,int pre,int sta,bool limit)
{
if(pos==-1)
return 1;
if(!limit&&dp[pos][pre]!=-1)
return dp[pos][pre];
int up=limit?a[pos]:9;
int i,tmp=0;
for(i=0;i<=up;i++)
{
if(sta==0&&i==0)
tmp+=Dfs(pos-1,12,0,limit&&i==a[pos]);
else if(fabs(pre-i)>=2)
tmp+=Dfs(pos-1,i,1,limit&&i==a[pos]);
}
if(!limit)
dp[pos][pre]=tmp;
return tmp;
}
int slove(int x)
{
int pos=0;
while(x)
{
a[pos++]=x%10;
x/=10;
}
return Dfs(pos-1,12,0,true);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,-1,sizeof(dp));
printf("%d\n",slove(m)-slove(n-1));
}
}