[Nowcoder] 数数字

题意:。。。咕咕懒得写了。

思路:
裸的记搜...

#include <bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,ll>mp[200010];
ll L,R;
inline int read() {
	int q=0,f=1;char ch = getchar();
	while(!isdigit(ch)){
		if(ch=='-')f=-1;ch=getchar();
	}
	while(isdigit(ch)){
		q=q*10+ch-'0';ch=getchar();
	}
	return q*f;
}
ll l,r,ans;
int a[31];
int top;
inline ll dfs(ll p,ll lim,ll s) {
	if(p == 0) {
		if(s == -1) {
			s = 0;
		}
		return L <= s && s <= R;
	}
	if(!lim && mp[p][s]) {
		return mp[p][s] - 1;
	}
	ll res = 0;
	int mx = lim ? a[p] : 9;
	for(int i = 0;i <= mx; ++i) {
		if(s == -1) {
			if(!i) {
				res += dfs(p - 1,lim && i == a[p],-1);
			}
			else res += dfs(p - 1,lim && i == a[p],i);
		}
		else {
			res += dfs(p - 1,lim && i == a[p],s * i);
		}
	}
	if(!lim) {
		mp[p][s] = res + 1;
	}
	return res;
}
inline ll work(ll x) {
	if(x == -1) {
		return 0;
	}
	else {
		top = 0;
		while(x) {
			a[++top] = x % 10;x /= 10;
		}
	}
	return dfs(top,1,-1);
}

int main () {
	scanf("%lld %lld %lld %lld",&l,&r,&L,&R);
	printf("%lld\n",work(r) - work(l - 1));
	return 0;
}


原文地址:https://www.cnblogs.com/akoasm/p/9614674.html