LOJ #10164 数字游戏【数位DP】

比windy数这道题要简单一点:

#include 
#include 
#include 
#include 
#include 
#define ll long long
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
#define repl(i,x,y) for(ll i=(x);i<(y);i++)
#define repd(i,x,y) for(ll i=(x);i>=(y);i--)
using namespace std;

const ll N=55;
const ll Inf=1e18;

ll x,y,w[N],f[N][N];

void split(ll x) {
	w[0]=0;
	while(x) {
		w[++w[0]]=x%10;x/=10;
	}w[w[0]+1]=0;
}

ll calc(ll x) {
	ll ans=0;
	
	split(x);
	
	repd(i,w[0],1) {
		if(w[i+1]>w[i]) break;
		repl(j,w[i+1],w[i]) ans+=f[i][j];
		if(i==1) ans++;
	}
	
	return ans;
}

int main() {
	rep(i,1,9) f[1][i]=1;
	
	repl(i,2,N) rep(j,0,9) rep(k,j,9) f[i][j]+=f[i-1][k];
	
	while(~scanf("%lld%lld",&x,&y)) printf("%lld\n",calc(y)-calc(x-1));
	
	return 0;
}

你可能感兴趣的:(LOJ,动态规划与递推,动态规划-树形DP)