UVA - 11076 Add Again (重复元素的排列)

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

 

Input

Each input set will start with apositive integerN (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 shouldbe a one line output containing the summation. The value will fit in 64-bitunsigned integer.

 

SampleInput                             Outputfor Sample Input

3

1 2 3

3

1 1 2

0

 

1332

444

 


Problemsetter: Md. Kamruzzaman

Special Thanks: Shahriar Manzoor

题意:输入n个数字,这些数字任何一种排列都是一个数,求所有整数的和

思路:每个数字固定在一位的话,那么其他位的排列数*这个数,就是这个数可能产生的数的大小,那么其实每个数都是一样的,最后再把它算到每一位上

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
typedef unsigned long long ll;
using namespace std;
const int maxn = 20;

int num[maxn], f[maxn];
int n;

void init() {
	f[0] = f[1] = 1;
	for (int i = 2; i < maxn; i++)
		f[i] = f[i-1] * i;
}

ll cal(int x) {
	ll tmp = 1;
	for (int i = 0; i < 10; i++)
		if (i == x)
			tmp *= f[num[i]-1];
		else tmp *= f[num[i]];
	return (ll)(f[n-1]) / tmp;
}

int main() {
	int a;
	init();
	while (scanf("%d", &n) != EOF && n) {
		memset(num, 0, sizeof(num));
		for (int i = 0; i < n; i++) {
			scanf("%d", &a);
			num[a]++;
		}
		ll sum = 0;
		for (int i = 0; i < 10; i++)
			if (num[i])
				sum += (ll)(i) * cal(i);
		ll ans = 0;
		for (int i = 0; i < n; i++)
			ans = ans * 10 + sum;
		printf("%lld\n", ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值