windy数(数位dp)

windy数

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

windy定义了一种windy数。

不含前导零且相邻两个数字之差至少为 2 的正整数被称为windy数。

windy想知道,在 A B 之间,包括 A B ,总共有多少个windy数?

Input

包含两个整数, A   B

满足  1AB2000000000  .

Output

Sample input and output

Sample Input Sample Output
1 10
9
dp[i][j]表示有i位的数字最高位为j的windy数有多少种。
确定最高位然后往后推。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
#define pi acos(-1)
using namespace std;
int dp[15][15];
void INIT(){
	memset(dp,0,sizeof(dp));
	for (int i=0;i<10;i++)
		dp[1][i]=1;
	for (int i=2;i<=10;i++)
		for (int j=0;j<10;j++)
			for (int k=0;k<10;k++)
				if (abs(j-k)>=2)
					dp[i][j]+=dp[i-1][k];
}
int go(int x){
	int b[15],t=0;
	while (x){
		b[++t]=x%10;
		x/=10;
	}
	b[t+1]=0;
	int ans=0;
	for (int i=1;i<t;i++)
		for (int j=1;j<10;j++)
			ans+=dp[i][j];
	for (int i=1;i<b[t];i++)
		ans+=dp[t][i];
	for (int i=t-1;i>0;i--){
		for (int j=0;j<b[i];j++)
			if (abs(j-b[i+1])>=2)
				ans+=dp[i][j];
		if (abs(b[i]-b[i+1])<2) break;
	}
	return ans;
}
int main(){
	INIT();
	int a,b;
	while(~scanf("%d%d",&a,&b))
		printf("%d\n",go(b+1)-go(a));
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值