题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2425
其实能够构成的数就是原数的排列(算前导0),然后组合计数一下就可以了。
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 50050
#define ll long long
using namespace std;
int cnt[],n;
ll ans,c[][];
char s[];
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
while (isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
ll cal(int x){
ll res=;
int now=x;
rep(j,,) {
res*=c[now][cnt[j]],now-=cnt[j];
}
return res;
}
int main(){
c[][]=;
rep(i,,){
c[i][]=;
rep(j,,i) c[i][j]=c[i-][j-]+c[i-][j];
}
scanf("%s",s+); n=strlen(s+);
rep(i,,n) ++cnt[s[i]-''];
rep(i,,n) {
rep(j,,(s[i]-'')-) if (cnt[j]){
--cnt[j];
ans+=cal(n-i);
++cnt[j];
}
--cnt[s[i]-''];
}
printf("%lld\n",ans);
return ;
}