码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-18 22:10:23      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:style   blog   os   io   strong   for   ar   2014   

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;
}


UVA - 11076 Add Again (重复元素的排列),布布扣,bubuko.com

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

标签:style   blog   os   io   strong   for   ar   2014   

原文地址:http://blog.csdn.net/u011345136/article/details/38665585

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!