注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

GreenCloudS

 
 
 

日志

 
 

BZOJ-1005: [HNOI2008]明明的烦恼(树的purfer编码)  

2014-01-26 17:20:00|  分类: oi,bzoj,树的purf |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

题目: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 ;
  • }


  评论这张
 
阅读(1)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018