标签:scoi2009 数位dp str += iostream algo limit ring bool
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=15;
int len;
int d[N];
int f[N][N];
int dfs(int pos,int pre,bool lead,int limit)
{
if(!pos)
return 1;
if(!limit && f[pos][pre]!=-1 && !lead)
return f[pos][pre];
int cnt=0;
int up=limit?d[pos]:9;
for(int i=0;i<=up;i++)
{
if(lead)
{
if(i==0)
cnt+=dfs(pos-1,pre,lead,limit&&d[pos]==i);
else
cnt+=dfs(pos-1,i,false,limit&&d[pos]==i);
}
else
{
if(abs(i-pre)<2)
continue;
else
cnt+=dfs(pos-1,i,false,limit&&d[pos]==i);
}
}
if(limit)
return cnt;
else
return f[pos][pre]=cnt;
}
int solve(int x)
{
len=0;
while(x)
{
d[++len]=x%10;
x/=10;
}
return dfs(len,-1,1,1);
}
int main()
{
int a,b;
cin>>a>>b;
memset(f,-1,sizeof f);
cout<<solve(b)-solve(a-1)<<endl;
return 0;
}
luogu P2657 [SCOI2009]windy数 数位dp入门题
标签:scoi2009 数位dp str += iostream algo limit ring bool
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12828128.html