bzoj 1026 [SCOI2009]windy数——数位dp水题

时间:2022-12-10 16:41:49

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1026

迷恋上用dfs写数位dp了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int l,r,dg[N],dp[N][N];
int dfs(int p,int lst,bool fr,bool fx)
{
if(!p)return ;
if(!fx&&dp[p][lst]!=-)return dp[p][lst];
int st=(fr?:),end=(fx?dg[p]:);
int ret=;
for(int i=st;i<=end;i++)
if(fr||(abs(i-lst)>=))
ret+=dfs(p-,i,,fx&(i==dg[p]));
if(!fx)dp[p][lst]=ret;
return ret;
}
int calc(int x)
{
int cnt=,ret=;
while(x)dg[++cnt]=x%,x/=;
for(int i=;i<=cnt;i++)
{
memset(dp,-,sizeof dp);//写在这里
ret+=dfs(i,,,(i==cnt));
}
return ret;
}
int main()
{
scanf("%d%d",&l,&r);
printf("%d\n",calc(r)-calc(l-));
return ;
}