洛谷P2657 [SCOI2009] windy数 数位dp

定义了不含前导 0 0 0但是任意相邻两数之差至少的 2 2 2的数为windy数,求问 A A A, B B B之前有多少这样的数。考虑 d p [ p o s ] [ p r e ] [ 0 ∣ 1 ] [ 0 ∣ 1 ] dp[pos][pre][0|1][0|1] dp[pos][pre][01][01] p o s pos pos位向后,前驱为 p r e pre pre,是否被高位限制,是否有前导 0 0 0的方案数。
当搜到某一位有前导 0 0 0限制的时候,可以把 − 2 -2 2作为前驱。这样枚举下一位时候 0 − 9 0-9 09恰好是合法的。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=LONG_LONG_MAX;
ll dp[11][10][2];
int a[11];
ll dfs(int pos,int pre,bool lim,bool zero) {
	if(pos==0) return 1;
	if(dp[pos][pre][lim]!=-1)
		return dp[pos][pre][lim]; 
	ll ans=0;
	int up=lim?a[pos]:9;
	for(int i=0;i<=up;i++) {
		if(abs(i-pre)<2) continue;
		ans+=dfs(pos-1,(zero&&i==0)?-2:i,lim&&i==a[pos],zero&&i==0);
	}
	return dp[pos][pre][lim]=ans; 
}
ll solve(ll x) {
	memset(dp,-1,sizeof(dp)); 
	int len=0;
	while(x) {
		a[++len]=x%10;
		x/=10; 
	}
	return dfs(len,-2,1,1);
}
int main() {
	ll l,r;
	scanf("%lld%lld",&l,&r);
	printf("%lld\n",solve(r)-solve(l-1)); 
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值