标签:组合游戏 add again uva11076 重复元素排序
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.
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.
For each test set, there should be a one line output containing the summation. The value will fit in 64-bit unsigned integer.
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 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]; } } 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 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; }
标签:组合游戏 add again uva11076 重复元素排序
原文地址:http://blog.csdn.net/u010579068/article/details/46432271