count 数字计数 HYSBZ - 1833 数位dp

给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次。

Input

输入文件中仅包含一行两个整数a、b,含义如上所述。

Output

输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。

Sample Input

1 99

Sample Output

9 20 20 20 20 20 20 20 20 20

Hint

30%的数据中,a<=b<=10^6;
100%的数据中,a<=b<=10^12


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 999999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 100003
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int a[50];
ll dp[40][500];
ll dfs(int pos, int lead, int limit, ll sum,int digit) {
	if (pos == 0)return sum;
	ll ans = 0;
	if (!limit&&lead && dp[pos][sum] != -1)return dp[pos][sum];
	int up = limit ? a[pos] : 9;
	for (int i = 0; i <= up; i++) {
		ans += dfs(pos - 1, lead || (i), limit && (i == up), sum + ((i == digit) && (lead || (i))), digit);
	}
	if (lead && !limit)dp[pos][sum] = ans;
	return ans;
}

ll sol(ll x, int digit) {
	int pos = 0; memset(dp, -1, sizeof(dp));
	while (x) {
		a[++pos] = x % 10; x /= 10;
	}
	return dfs(pos, 0, 1, 0, digit);
}

int main()
{
	//ios::sync_with_stdio(false);
	ll a, b; rdllt(a); rdllt(b);
	for (int i = 0; i <= 9; i++) {
		cout << sol(b, i) - sol(a - 1, i) << ' ';
	}
    return 0;
}

 

你可能感兴趣的:(bzoj,NOI/NOIp)