BZOJ2425 [HAOI2010]计数 【数位dp】

题目

你有一组非零数字(不一定唯一),你可以在其中插入任意个0,这样就可以产生无限个数。比如说给定{1,2},那么可以生成数字12,21,102,120,201,210,1002,1020,等等。

现在给定一个数,问在这个数之前有多少个数。(注意这个数不会有前导0).

输入格式

只有1行,为1个整数n.

输出格式

只有整数,表示N之前出现的数的个数。

输入样例

1020

输出样例

7

提示

n的长度不超过50,答案不超过(2^{63}-1).

题解

如果我们看做把0删除看做把0前导,那么问题就转化成了求所有数的排列中比当前数小的个数
我们只需统计当前(i)位相同,第(i + 1)位比原数小时有多少种情况
那么剩余的位就可以随便排列了,用带重复元素的排列(frac{N!}{n1!*n2!*n3!......})
当然可能会爆long long,可以对阶乘质因子分解来计算

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 55,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
int num[maxn],n,isn[maxn];
LL a[10],fac[maxn],p[maxn],pi,ans;
void init(){
	for (int i = 2; i < maxn; i++){
		if (!isn[i]) p[++pi] = i;
		for (int j = 1; j <= pi && i * p[j] < maxn; j++){
			isn[i * p[j]] = true;
			if (i % p[j] == 0) break;
		}
	}
}
LL Cal(LL x,LL t){
	LL re = 0;
	while (x / t) re += (x /= t);
	return re;
}
LL qpow(LL a,LL b){
	LL re = 1;
	for (; b; b >>= 1,a = a * a)
		if (b & 1) re = re * a;
	return re;
}
LL cal(){
	LL re = 1,tot = 0;
	for (int i = 0; i < 10; i++) tot += a[i];
	for (int i = 1; i <= pi && p[i] <= tot; i++){
		LL cnt = Cal(tot,p[i]);
		for (int j = 0; j < 10; j++) cnt -= Cal(a[j],p[i]);
		re = re * qpow(p[i],cnt);
	}
	return re;
}
int main(){
	init();
	char c;
	while ((c = getchar()) != EOF){
		if (!isdigit(c)) break;
		num[++n] = c - '0';
		a[num[n]]++;
	}
	
	for (int i = 1; i <= n; i++){
		for (int j = 0; j < num[i]; j++){
			if (!a[j]) continue;
			a[j]--;
			ans += cal();
			a[j]++;
		}
		a[num[i]]--;
	}
	cout << ans << endl;
	return 0;
}

原文地址:https://www.cnblogs.com/Mychael/p/8734198.html