排列之和 UVa11076

1.题目描述:点击打开链接

2.解题思路:本题利用平均数的思想解决。由于每个数出现在任何一位的总的次数都是相同的,因此可以等效为它们的平均数出现的次数,而出现的次数就是重复排列的组合数,最后再乘以n个1即可得到答案。比如一个序列是{1,1,2},那么平均数就是(1+1+2)/3=4/3。出现的次数就是P(3,3)/P(2,2)=3,一共有3个1,那么ans=(4/3)*3*111=444。

3.代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<long long, long long> PL;
#define me(s) memset(s,0,sizeof(s))
#define For(i,n) for(int i=0;i<(n);i++)


const ll one[13] =
{
	0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111,
	111111111, 1111111111, 11111111111, 111111111111
};

ll a[10], fac[13];//factorial

int main(void)
{
	//freopen("t.txt", "r", stdin);
	int n, num, count;
	long long ans;
	fac[0] = 1;
	for (int i = 1; i <= 12; i++)
		fac[i] = i * fac[i - 1];///计算阶乘
	while (scanf("%d", &n), n)
	{
		memset(a, 0, sizeof(a));
		count = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &num);
			count += num;
			a[num]++;
		}
		ans = fac[n - 1] * count;//除以n的部分和n!约分,得到(n-1)!
		for (int i = 0; i < 10; ++i)
			ans /= fac[a[i]];
		printf("%lld\n", ans * one[n]);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值