UVa11076 - Add Again

Problem C
Add Again
Input: 
Standard Input

Output: Standard Output

 

Summation of sequence of integers is always a common problem in Computer Science. Rather than computing blindly, some intelligent techniques make the task simpler. Here you have to find the summation of a sequence of integers. The sequence is an interesting one and it is the all possible permutations of a given set of digits. For example, if the digits are <1 2 3>, then six possible permutations are <123>, <132>, <213>, <231>, <312>, <321> and the sum of them is 1332.

 

Input

Each input set will start with a positive integer N (1≤N≤12). The next line will contain N decimal digits. Input will be terminated by N=0. There will be at most 20000 test set.

 

Output

For each test set, there should be a one line output containing the summation. The value will fit in 64-bit unsigned integer.

 

Sample Input                             Output for Sample Input

3

1 2 3

3

1 1 2

0

 

1332

444

 



#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 10;

int cnt[N];
int n;

LL factorial(int x);
LL f(int x);

void solve();
bool input();

int main()
{
#ifndef ONLINE_JUDGE
    freopen("e:\\uva_in.txt", "r", stdin);
#endif

    while (input()) {
        solve();
    }
    return 0;
}

bool input()
{
    if (scanf("%d", &n) != 1 || n == 0) return false;

    memset(cnt, 0x00, sizeof(cnt));

    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        cnt[x]++;
    }

    return true;
}

LL factorial(int x)
{
    if (x == 0 || x == 1) return 1LL;
    else return (LL)x * factorial(x - 1);
}

LL f(int x)
{
    LL ans = 0;
    for (int i = 0; i < n; i++) {
        ans = ans * 10 + x;
    }

    return ans;
}

void solve()
{
    LL num = factorial(n);

    for (int i = 0; i < N; i++) {
        if (cnt[i] != 0) {
            num /= factorial(cnt[i]);
        }
    }

    LL ans = 0;
    for (int i = 0; i < N; i++) {
        if (cnt[i] != 0) {
            ans += f(i) * num * cnt[i] / n;
        }
    }

    printf("%lld\n", ans);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值