题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1005
树有种神奇的东西叫做purfer编码,每个编码对应y一棵树,然后就可以排列组合啦~
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std ;
#define MAXN 10010
int p[ MAXN ] ;
void Init( ) {
bool f[ MAXN ] ;
memset( f , true , sizeof( f ) ) ;
f[ 0 ] = f[ 1 ] = false , p[ 0 ] = 0 ;
for ( int i = 1 ; i ++ < MAXN - 1 ; ) if ( f[ i ] ) {
p[ ++ p[ 0 ] ] = i ;
for ( int j = i << 1 ; j < MAXN ; j += i ) f[ j ] = false ;
}
}
int n , sum = 0 , d[ MAXN ] , cnt = 0 ;
int num[ MAXN ] ;
void INC( int x ) {
for ( int i = 0 ; i ++ < p[ 0 ] ; ) {
if ( x == 1 ) break ;
while ( ! ( x % p[ i ] ) ) ++ num[ i ] , x /= p[ i ] ;
}
}
void DEC( int x ) {
for ( int i = 0 ; i ++ < p[ 0 ] ; ) {
if ( x == 1 ) break ;
while ( ! ( x % p[ i ] ) ) -- num[ i ] , x /= p[ i ] ;
}
}
struct bigint {
int a[ MAXN ] , m ;
bigint( ) {
memset( a , 0 , sizeof( a ) ) ;
a[ 1 ] = m = 1 ;
}
void print( ) {
printf( "%d" , a[ m ] ) ;
for ( int i = m - 1 ; i ; -- i ) {
if ( a[ i ] < 1000 ) printf( "0" ) ;
if ( a[ i ] < 100 ) printf( "0" ) ;
if ( a[ i ] < 10 ) printf( "0" ) ;
printf( "%d" , a[ i ] ) ;
}
printf( "\n" ) ;
}
void multi( int x ) {
for ( int i = 0 ; i ++ < m ; ) {
a[ i ] *= x ;
}
int M = m ;
for ( int i = 0 ; i ++ < m ; ) {
for ( int j = i ; a[ j ] >= 10000 ; ++ j ) {
a[ j + 1 ] += a[ j ] / 10000 ;
a[ j ] %= 10000 ;
M = max( M , j + 1 ) ;
}
}
m = M ;
}
} ans ;
int main( ) {
Init( ) ;
scanf( "%d" , &n ) ;
for ( int i = 0 ; i ++ < n ; ) {
int x ; scanf( "%d" , &x ) ;
if ( n == 1 ) {
if ( ! x ) printf( "1\n" ) ; else printf( "0\n" ) ;
return 0 ;
}
if ( x != - 1 ) sum += ( x - 1 ) , d[ ++ cnt ] = x - 1 ;
}
if ( sum > n - 2 ) {
printf( "0\n" ) ; return 0 ;
}
if ( n == cnt ) {
printf( "1\n" ) ; return 0 ;
}
memset( num , 0 , sizeof( num ) ) ;
for ( int i = 0 ; i ++ < n - 2 ; ) INC( i ) ;
for ( int i = 0 ; i ++ < n - sum - 2 ; ) INC( n - cnt ) ;
for ( int i = 0 ; i ++ < cnt ; ) {
for ( int j = 0 ; j ++ < d[ i ] ; ) DEC( j ) ;
}
for ( int i = 0 ; i ++ < n - sum - 2 ; ) DEC( i ) ;
for ( int i = 0 ; i ++ < p[ 0 ] ; ) {
for ( int j = 0 ; j ++ < num[ i ] ; ) ans.multi( p[ i ] ) ;
}
ans.print( ) ;
return 0 ;
}