uva 11076

时间:2022-12-24 15:13:39

计算出每一位上数字i会出现的次数  累加

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 1010
#define INF 0x7fffffff
#define inf 10000000
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
using namespace std; int a[10], b[10], sum, f[13];
void init()
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
sum = 0;
f[0] = f[1] = 1;
for(int i = 2; i < 13; ++ i)
f[i] = f[i-1]*i;
} int main()
{
int n, r;
while(scanf("%d", &n) == 1 && n)
{
init();
for(int i = 0; i < n; ++ i) {scanf("%d", &r); a[r] ++; sum ++;}
for(int i = 1; i < 10; ++ i)
if(a[i]){
b[i] = f[sum-1];
for(int j = 0; j < 10; ++ j)
if(a[j])
b[i] /= i == j ? f[a[j]-1]:f[a[j]];
}
ll ans = 0;
for(int i = 0; i < n; ++ i)
{
ans *= 10;
for(int j = 0; j < 10; ++ j)
ans += j*b[j];
}
printf("%lld\n", ans);
}
return 0;
}