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

UVA 11076 Add Again(有重复元素的全排列问题)

2015-10-16 15:09 525 查看

UVA 11076 Add Again

题意:

输入N个数字,求这几个数字组成的数之和。

如:1,2,3.组成的数有123,132,213,231,312,321,和为1332.

思路:

有重复元素的全排列类问题:

有k个元素,第 i 个元素 ai 有 ci 个,设总的全排列个数为 x。

那么:设共有 n 个元素(n=∑ai),得方程 a1!a2!⋯ak!∗x=n!

得到全排列个数 x ,进而得到每一个数字在每一位出现的次数为 x / n。

例如:

1,2,2每一个数字在每一位出现的次数为 3/3 = 1;

1,2,3每一个数字在每一位出现的次数为 6/3 = 2。

回到求和问题,

<1,2,3>组成的数字有:

123

132

213

231

312

321

sum = (6 * 2)(6 * 2)(6 * 2)

换成十进制即 sum = 12*10^2 + 12 * 10 + 12 = 1332。

综上,

答案的每一位 = 所有数字之和乘以出现次数,

故 sum=x/n∗∑aici∗11..11(n个1)

代码:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <functional>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <bitset>

using namespace std;

#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0)
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define pr( x ) cout << #x << " = " << x << endl
#define pri( x ) cout << #x << " = " << x << " "
#define test( t ) int t ; cin >> t ; int kase = 1 ; while( t-- )
#define out( kase ) printf( "Case %d: " , kase++ )
#define mp make_pair
#define pii pair<int,int>
#define pli pair<long long,int>
#define pll pair<long long,long long>
#define fs first
#define se second
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ULL;
const double eps = 1e-8 ;
const int inf = 0x3f3f3f3f ;
const long long INF = 0x3f3f3f3f3f3f3f3fLL ;

// const int N = ;
int num[20] , a[20] ;
lint fac[20] ;
lint basic[] = { 1 , 11 , 111 , 1111 , 11111 , 111111 , 1111111 , 11111111 , 111111111 ,1111111111 ,11111111111 , 111111111111 } ;
int n ;
void init()
{
fac[0] = 1 ;
for ( int i = 1 ; i < 20 ; i++ )
fac[i] = fac[i-1] * i ;

}
void work()
{
lint ans = 0 ;
cls( num ) ;
for ( int i = 0 ; i < n ; i++ ) {
scanf( "%d" , a + i ) ;
ans += a[i] ;
num[a[i]] ++ ;
}
lint x = fac
;
for (int i = 0; i < 10 ; i++) {
x /= fac[num[i]] ;
}
ans = ans * x * basic[n-1] / n ;
cout << ans << endl ;
}
int main()
{
// freopen("my.in","r",stdin);
// freopen("my.out","w",stdout);
init() ;
while( cin >> n && n ) {
work() ;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva