1. 程式人生 > >hdu 1261 字串數 排列組合

hdu 1261 字串數 排列組合

#include <stdio.h>
#include <string.h>
#define SIZE 30
typedef long long ll ;
int d[SIZE] ;
int ans[1000] , f[15];
void multiply(int c)
{
	ans[0] = ans[1] = 1 ;
	for(int i = 2 ; i <= c ; ++i)
	{
		int r = 0 ;
		for(int j = 1 ; j <= ans[0] ; ++j)
		{
			ans[j] *= i ; 
			ans[j] += r ;
			r = ans[j]/10 ; 
			ans[j] %= 10  ; 
		}
		if(r != 0)
		{
			while(r)
			{
				ans[ans[0]+1] += r%10 ;
				ans[0] = ans[0]+1 ;
				r /= 10 ;
			}
		}
	}
}

void divide(int n)
{
	for(int i = 0 ; i < n ; ++i)
	{
		if(d[i] == 1)
			continue ;
		ll r = 0 ;
		for(int j = ans[0] ; j > 0 ; --j)
		{
			r = r*10 + ans[j] ;
			ans[j] = (int)(r/f[d[i]]) ;
			r %= f[d[i]] ;
		}
		int j = ans[0] ;
		while(!ans[j--]) ;
		ans[0] = j+1 ;
	}
}

int main()
{
	int n ;
	f[0] = f[1] = 1 ;
	for(int i = 2 ; i < 15 ; ++i)
		f[i] = f[i-1]*i ;
	while(scanf("%d",&n) && n)
	{
		int c = 0; 
		memset(ans,0,sizeof(ans)) ;
		for(int i = 0 ; i < n ; ++i)
		{
			scanf("%d",&d[i]) ;
			c += d[i] ;
		}
		multiply(c) ; 
		divide(n) ;
		for(int i = ans[0] ; i > 0 ; --i)
			printf("%d",ans[i]) ;
		puts("") ;
	}
	return 0 ;
}
/*
26
12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12

答案:
 4304048360075613709828073573963439771561246234874986506307801024608438578682946287839555537779695114
6973463603476517994060086036378690699890922398850719933177533763394328048206442072300423203375709346
9667364765509110256937684504670401569909337603281490150010206952984854343148131819790936625546951803
7655784189390500543894622277567223351757133619614420271316220121236818547225642751651682556313600000
0000000000000000000
*/

與君共勉