您的位置:首页 > 大数据 > 人工智能

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 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

[align=center][/align]
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: