Uva 11076 Add Again 解题报告(组合数学)
2014-03-24 18:22
417 查看
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.
Problemsetter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor
解题报告:假设不去重,那么n个数一共有n!种组合。以第一位为例,n个数各出现(n-1)!次,那么第一位的和就是(n-1)!*sum。再去重,每个数出现p次,结果就除以p!。最后统计成一个数字即可。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL factorial[20];
void init()
{
factorial[0]=1;
for(int i=1;i<=12;i++)
factorial[i]=factorial[i-1]*i;
}
int d[10];
int main()
{
init();
int n;
while(~scanf("%d",&n) && n)
{
memset(d,0,sizeof(d));
LL ans = factorial[n-1];
int sum = 0;
for(int i=0;i<n;i++)
{
int tmp;
scanf("%d",&tmp);
d[tmp]++;
sum+=tmp;
}
ans*=sum;
for(int i=0;i<10;i++)
ans/=factorial[d[i]];
LL res=0;
for(int i=0;i<n;i++)
res = res*10+ans;
printf("%lld\n", res);
}
}
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 |
Special Thanks: Shahriar Manzoor
解题报告:假设不去重,那么n个数一共有n!种组合。以第一位为例,n个数各出现(n-1)!次,那么第一位的和就是(n-1)!*sum。再去重,每个数出现p次,结果就除以p!。最后统计成一个数字即可。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL factorial[20];
void init()
{
factorial[0]=1;
for(int i=1;i<=12;i++)
factorial[i]=factorial[i-1]*i;
}
int d[10];
int main()
{
init();
int n;
while(~scanf("%d",&n) && n)
{
memset(d,0,sizeof(d));
LL ans = factorial[n-1];
int sum = 0;
for(int i=0;i<n;i++)
{
int tmp;
scanf("%d",&tmp);
d[tmp]++;
sum+=tmp;
}
ans*=sum;
for(int i=0;i<10;i++)
ans/=factorial[d[i]];
LL res=0;
for(int i=0;i<n;i++)
res = res*10+ans;
printf("%lld\n", res);
}
}
相关文章推荐
- Uva 11609 - Teams 解题报告(组合数学)
- UVa 11076 - Add Again (排列之和 组合数学)
- UVA 11076 - Add Again(组合)
- BestCoder Round #35(第一题解题报告(组合数学(期望的可加性)或暴力枚举)
- 解题报告_Triangle Counting UVA - 11401_数学规律
- Uva 10780 Again Prime? No time. 解题报告(数学)
- Uva 1319 - Maximum 解题报告(数学)
- UVa 11076 Add Again(数学)
- Uva 12050 (POJ 2402) - Palindrome Numbers 解题报告(数学)
- BZOJ 1008 组合数学 解题报告
- Uva 1388 Graveyard 解题报告(数学)
- BZOJ 3997 [TJOI 2015 组合数学] 解题报告
- 【ACM训练计划】 数论、组合数学 好题精选+解题报告
- BZOJ3997: [TJOI2015]组合数学 解题报告
- UVALive 6582 Magical GCD (数学+数据结构) -- 解题报告
- 【ACM训练计划】 数论、组合数学 好题精选+解题报告
- Uva 11752 The Super Powers 解题报告(数学)
- Uva 1073 - Glenbow Museum 解题报告(组合)
- UVA 10420 解题报告
- Leetcode 474. Ones and Zeroes 01组合 解题报告