排列组合
求所有情况相加之后的和.
这里要对每一个数字在每一位上出现的次数进行求解。这里用到了高中知识
对于相同重复元素的排列组合。
加入统计数字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;
}