UVA11076 Add Again 排列组合

排列组合

求所有情况相加之后的和.

这里要对每一个数字在每一位上出现的次数进行求解。这里用到了高中知识

对于相同重复元素的排列组合。

加入统计数字1出现的次数 假设0到9每个数次出现ni次

则数字1在固定·一位上出出现的次数 为     n!/(n0!*(n1-1)!*....*n9!)  最后结果就是1在当前位出现的次数。我们可以知道对于每一位每一个数字出现的情况都是相同的。只要求出一位的情况其他都相同,只要*10累加即可。注意答案范围。



Problem C
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 integer N (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


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

typedef unsigned long long ULL;

ULL f[13];
ULL a[13],n,ans,sum;
int cnt[10];
int main()
{
    f[0]=1;
    for(int i=1;i<=12;i++) f[i]=f[i-1]*i;
    while(cin>>n&&n)
    {
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            cnt[a[i]]++;
            if(cnt[a[i]]!=1) a[i]=-1;
        }
        sum=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]==-1) continue;
            ULL tmp=f[cnt[a[i]]-1];
            for(int j=0;j<n;j++)
            {
                if(i==j) continue;
                if(a[j]==-1) continue;
                tmp*=f[cnt[a[j]]];
            }
            tmp=f[n-1]/tmp;
            sum+=(tmp*a[i]);
        }
        ans=0;
        for(int i=0;i<n;i++)
        {
            ans+=sum;
            sum*=10;
        }
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值