终于明白数位dp了,就是一个模板,直接贴代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define md
#define ll long long
#define inf (int) 1e9
#define eps 1e-8
#define N
using namespace std;
int f[15][15][2];
int a[15];
char st[15];
int work(int x)
{
int n=0;
while (x)
{
a[++n]=x%10; x/=10;
}
reverse(a+1,a+n+1);
//for (int i=1;i<=n;i++) printf("%d ",a[i]); printf("\n");
memset(f,0,sizeof(f));
for (int i=1;i<=a[1];i++) f[1][i][i==a[1]]=1;
for (int i=2;i<=n;i++)
{
for (int j=0;j<=9;j++)
for (int k=0;k<=9;k++)
if ((j-k)*(j-k)>=4) f[i][j][0]+=f[i-1][k][0];
for (int j=0;j<=a[i];j++)
for (int k=0;k<=9;k++)
if ((j-k)*(j-k)>=4) f[i][j][j==a[i]]+=f[i-1][k][1];
}
int ans=0;
for (int i=0;i<=9;i++) ans+=f[n][i][0]+f[n][i][1];
memset(f,0,sizeof(f));
for (int i=1;i<=9;i++) f[1][i][0]=1;
for (int i=2;i<=n-1;i++)
{
for (int j=0;j<=9;j++)
for (int k=0;k<=9;k++)
if ((j-k)*(j-k)>=4) f[i][j][0]+=f[i-1][k][0];
}
for (int i=1;i<=n-1;i++)
for (int j=0;j<=9;j++)
ans+=f[i][j][0];
return ans;
}
int main()
{
int d;
scanf("%d",&d); d--;
int ans1=work(d); //printf("ans:%d\n",ans1);
scanf("%d",&d);
int ans2=work(d); //printf("ans:%d\n",ans2);
printf("%d\n",ans2-ans1);
return 0;
}