UVA 11076 Add Again

题目链接:UVA-33478

题意为给定n个数,求这n个数能组成的所有不同的排列组成的数字的和。

思路:发现对于任意一个数字,其在每一位出现的次数是相同的。换言之,所有数字的每一位相加的和是相同的。

所以我们只需求出这个“和”即可。

考虑任意一位i,假设我们在i位放置x,则对应\( (n-1)! / ( d_0! * d_1! * ... * d_x! * ... * d_9! ) \)种情况。

所以我们要求的“和”等于\(\sum_x x * (n-1)! / ( d_0! * d_1! * ... * d_x! * ... * d_9! )\)。

代码如下:

 1 #include"cstdio"
 2 #include"iostream"
 3 #include"cstring"
 4 #include"algorithm"
 5 #include"cstdlib"
 6 #include"vector"
 7 #include"set"
 8 using namespace std;
 9 typedef unsigned long long LL;
10 const LL MAXN=1e5;
11
12 LL fact[20];
13 int main()
14 {
15 #ifdef LOCAL
16     freopen("in.txt","r",stdin);
17     //freopen("out.txt","w",stdout);
18 #endif
19     fact[0]=1;
20     for(LL i=1;i<13;i++)
21         fact[i]=fact[i-1]*i;
22     LL n;
23     while(scanf("%lld",&n)!=EOF && n)
24     {
25         LL d[10];
26         memset(d,0,sizeof(d));
27         for(LL i=1;i<=n;i++)
28         {
29             LL tmp;
30             scanf("%lld",&tmp);
31             d[tmp]++;
32         }
33         LL r=0;
34         for(LL i=0;i<10;i++)
35             if(d[i])
36             {
37                 LL tmp=fact[n-1];
38                 for(LL j=0;j<10;j++)
39                 {
40                     if(j==i) tmp/=fact[d[j]-1];
41                     else tmp/=fact[d[j]];
42                 }
43                 r+=tmp*i;
44             }
45         LL ans=0;
46         for(LL i=1;i<=n;i++)
47         {
48             ans*=10;
49             ans+=r;
50         }
51         printf("%lld\n",ans);
52     }
53     return 0;
54 }
时间: 2024-10-18 23:21:25

UVA 11076 Add Again的相关文章

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 interes

UVA 11076 Add Again 计算对答案的贡献+组合数学

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, thenumber of di?erent integer pairs with LCM is equal to N

【数论】UVa 11076 - Add Again

Add AgainInput: 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 summa

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

题意:给你N个数,求把他们的全排列加和为多少 思路:对于这道题,假设数字k1在第一位,然后求出剩下N-1位的排列数num1,我们就可以知道k1在第一位时的排列有多少种为kind1, 同理,假设数字k2在第一位然后求出剩下N-1位的排列数num2,我们就可以知道k2在第一位时的排列有多少种为kind2, k1*num1+k1*num2.....+kn*numn 就是我们要求的这些数对第一位的所有贡献,我们知道第一位的贡献=对第二位的贡献=第三位的贡献..... 把所有贡献加和,就能求出结果 知识:

uva 10954 Add All(排序)

uva 10954 Add All Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to write a C/C++ program just to add a set of numbers. Such a problem will simply question your erudition. So, let's add

UVA - 10954 - Add All (贪心)

UVA - 10954 Add All Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Add All Input: standard input Output: standard output Yup!! The problem name reflects your task; just add a set of numbers.

【NOIP合并果子】uva 10954 add all【贪心】——yhx

Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvescondescended, to write a C/C++ program just to add a set of numbers. Such a problem will simplyquestion your erudition. So, lets add some avor of ingenuity

UVA 11040 Add bricks in the wall

https://vjudge.net/problem/UVA-11040 找规律 #include<cstdio> using namespace std; int a[10][10]; int main() { int T,d; scanf("%d",&T); while(T--) { for(int i=1;i<=5;i++) for(int j=1;j<=i;j++) scanf("%d",&a[i*2-1][j*2-1

uva 10954 Add All(哈弗曼编码)

这道题我一开始想错了,这么简单的题都wa了两发...我往贪心上面想了,每次都找一个最小的数相加,结果就是 排序后直接往后加,还在那纳闷为何出错...其实这道题是哈弗曼编码问题,简直是模板题目,就是每次找两个最 小的结点求和后把他们的和放到节点中去,把这两个点删除...用的multiset,其实和set容器差不多,就是可 以存放重复的元素... 代码: #include<iostream> #include<cstdio> #include<cstdlib> #inclu