Add Again(重复元素排序) UVA11076
2015-06-09 23:56
429 查看
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 positiveintegerN (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 lineoutput 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 |
Problemsetter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor
思路:对于第x位,一共有k个元素,其中第i个元素有ni个,求全排列个数:全排列个数=(n-1)!/(n1!*n2!*n3!*n4!..(ni-1)!..*nk!)算出每个数出现在每个位置的次数,然后乘以i加起来就是i元素的贡献值了
转载请注明出处:
寻找&星空の孩子
#include<stdio.h> #include<string.h> #define LL unsigned long long int a[10];// 题目没有说很清楚,是0-9之间的数; int b[15]; LL jie[15]; int N; void init() { jie[0]=1; for(LL i=1;i<15;i++) { jie[i]=i*jie[i-1]; } } LL chsort(LL x) { LL cnt=1; for(int i=0;i<10;i++) { if(a[i]) { if(i==x) cnt*=jie[a[i]-1]; else cnt*=jie[a[i]]; } } return cnt; } int main() { // freopen("Add.txt","r",stdin); LL ans,sum; init(); while(scanf("%d",&N),N) { sum=0; memset(a,0,sizeof(a)); for(int i=0;i<N;i++) { int tp; scanf("%d",&tp); a[tp]++; // sum+=b[i]; } ans=0; // ans=jie[N-1]*sum; for(LL i=0;i<10;i++) { if(a[i]) { ans+=jie[N-1]*i/chsort(i); } } LL kk=0; for(int i=1;i<=N;i++) { kk=kk*10+ans; } printf("%llu\n",kk); } return 0; }
相关文章推荐
- There is an overlap in the region chain修复
- There is an overlap in the region chain
- 480000 millis timeout while waiting for channel to be ready for write异常处理
- http://musicbusiness.baijia.baidu.com/article/76737
- Add Again(重复元素排序)
- pip install lxml 报错error: command 'gcc' failed wit
- LightOJ1021---Painful Bases (状压dp)
- LightOJ 1038 - Race to 1 Again 【DP】
- flume-ng负载均衡load-balance、failover集群搭建
- 关于informatica 961Domain库不支持GBK 出现的一系列问题
- HDU1788 Chinese remainder theorem again 最小公倍数
- Container With Most Water——双指针
- LeetCode之“动态规划”:Climbing Stairs
- org.tinygroup.beancontainer-IOC、AOP框架
- POJ1995 Raising Modulo Numbers 快速幂取模
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- HCE AID选择策略测试
- http://blog.csdn.net/lenotang/article/details/2823230
- leetcode_Factorial Trailing Zeroes_easy
- Leetcode[219]-Contains Duplicate II