Add Again(重复元素排序)
来源:互联网 发布:社交软件源码 编辑:程序博客网 时间:2024/05/17 23:47
Add Again
Input: 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 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 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 should be a one line output 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
/*题意大概就是给你n个数,用这n个数能组成m个整数,求m个整数的和这题知识点就一个就是有重复元素的全排列个数:有k个元素,其中第i个元素有ni个,求全排列个数:全排列个数=n!/(n1!*n2!*n3!*n4!....*nk!)算出每个数出现在每个位置的次数,然后加起来就可以了ps:这道题会卡long long 要用unsigned long long ,貌似uva以前的题经常有这种蛋疼的问题啊*/
#include<stdio.h>#include<string.h>#define LL unsigned long longint 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]; }}int main(){ freopen("Add.txt","r",stdin); LL ans,sum,tp; 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+=tp; } ans=jie[N-1]*sum; for(LL i=0;i<10;i++) { if(a[i]) ans/=jie[a[i]]; } LL kk=0; for(int i=1;i<=N;i++) { kk=kk*10+ans; } printf("%llu\n",kk); } return 0;}
对于第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 longint 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;}
- Add Again(重复元素排序)
- UVA 11076 Add Again(重复元素排列)
- UVA - 11076 Add Again (重复元素的排列)
- UVA 11076 Add Again(有重复元素的全排列问题)
- ArrayList.add()添加重复元素
- 排序单链表去重复元素
- Add Again
- 重复元素排序问题--递归
- 删除排序链表中重复元素
- Uva11076-Add Again
- UVA 11076 - Add Again
- UVA 11076 Add Again
- UVa:11076 Add Again
- UVA 11076 Add Again
- UVa11076 - Add Again
- uva 11076 Add Again
- UVA 11076 Add Again
- UVA 11076-Add Again
- [CodeForces445A]DZY Loves Chessboard[bfs]
- Add Binary
- Uncaught Error: Bootstrap's JavaScript requires jQuery
- POJ - 3692 Kindergarten 二分图 最大匹配
- 微分方程(1)---微分方程的数值解法
- Add Again(重复元素排序)
- 带外数据OOB与紧急模式URG
- JavaScript实现禁用键盘和鼠标的点击事件
- hdu 5234 Happy birthday(格子图上的01背包)
- ListView 的标准写法
- C++继承时派生类与基类有同名函数时如何分别引用
- LeetCode——Palindrome Number
- 自己动手解Json!(IOS利用分治法解析Json)(一)
- Eclipse里面找不到DatabaseExplorer,怎么办?