UVa11076

Posted Yan_Bin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa11076相关的知识,希望对你有一定的参考价值。

11076 Add Again
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
31
2 3
31
1 2
0
Sample Output
1332
444

题意:
       输入n个数字,这些数字的任意排列总是一个数。你的任务是求出所有这些数的和。

分析:

       其实,输入的每个数字在每一位出现的概率都是相同的,于是,我们可以先计算出所有输入数字的平均值A。而可重复的排列数就是P=n!/(n1!*n2!*…*nk!)最终的答案就是11…1(n个1)*A*P。

 1 #include <cstdio>
 2 #include <cstring>
 3 #define ll long long
 4 int N;
 5 ll factorial[13];
 6 int num[10];
 7 const ll one[13] = {
 8     0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111,
 9     111111111, 1111111111, 11111111111, 111111111111
10 };
11 void cal_factorial(){
12     factorial[0] = 1,factorial[1] = 1;
13     for(int i = 2 ; i <= 12 ; i++) factorial[i] = i * factorial[i - 1];
14 }
15 int main(){
16     cal_factorial();
17     int sum = 0;
18     while(scanf("%d",&N) == 1 && N){
19         sum = 0;
20         memset(num,0,sizeof num);
21         for(int i = 0 ; i < N ; i++){
22             int tmp; scanf("%d",&tmp);
23             num[tmp]++,sum += tmp;
24         }
25         ll ans = factorial[N - 1] * sum;
26         for(int i = 0 ; i < 10 ; i++) ans /= factorial[num[i]]; // 平均数需要除以N,将分子上N
27         printf("%lld\\n",ans * one[N]);
28     }
29     return 0;
30 }
View Code

 

以上是关于UVa11076的主要内容,如果未能解决你的问题,请参考以下文章

Uva 11076 Add Again (数论+组合数学)

11076: 小P的集合 位运算

UVa 1442 - Cave

UVa10410代码

UVa 1593代码对齐

UVa 1593 代码对齐