bzoj1026:[SCOI2009]windy数
传送门
普通数位dp
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e5+10;
int n,m,f[50][10],a[20];
void prepare()
{
for(rg int i=0;i<=9;i++)f[1][i]=1;
for(rg int i=2;i<=11;i++)
for(rg int j=0;j<=9;j++)
for(rg int k=0;k<=9;k++)
if(abs(j-k)>=2)f[i][k]+=f[i-1][j];
}
int solve(int x)
{
int len=0,ans=0;
while(x)len++,a[len]=x%10,x/=10;
for(rg int i=1;i<len;i++)
for(rg int j=1;j<=9;j++)ans+=f[i][j];
for(rg int i=1;i<a[len];i++)ans+=f[len][i];
for(rg int i=len-1;i>=1;i--)
{
for(rg int j=0;j<a[i];j++)
if(abs(j-a[i+1])>=2)ans+=f[i][j];
if(abs(a[i]-a[i+1])<2)break;
if(i==1)ans++;
}
return ans;
}
int main()
{
read(n),read(m);prepare();
printf("%d\n",solve(m)-solve(n-1));
}