BZOJ 1005: [HNOI2008]明明的煩惱 Purfer序列 大數

來源:互聯網
上載者:User

標籤:

1005: [HNOI2008]明明的煩惱

Time Limit: 20 Sec

Memory Limit: 256 MB

題目串連

http://www.lydsy.com/JudgeOnline/problem.php?id=1005

Description

自從明明學了樹的結構,就對奇怪的樹產生了興趣...... 給出標號為1到N的點,以及某些點最終的度數,允許在任意兩點間連線,可產生多少棵度數滿足要求的樹?

Input

第一行為N(0 < N < = 1000),接下來N行,第i+1行給出第i個節點的度數Di,如果對度數不要求,則輸入-1

Output

一個整數,表示不同的滿足要求的樹的個數,無解輸出0

Sample Input

3
1
-1
-1

Sample Output

2

HINT

 

題意

 

題解:

實際上就是讓你求,有多少種Purfer序列

理解Purfer序列之後,這就是排列組合題了

這篇博文寫的非常精彩:http://www.cnblogs.com/zhj5chengfeng/archive/2013/08/23/3278557.html

代碼:

import java.util.*;import java.math.*;public class Main {    static int n, d[]=new int[10002];    static BigInteger p[]=new BigInteger[1002];    static BigInteger ans;        static public void main(String args[]) {        Scanner IN=new Scanner(System.in);        n=IN.nextInt();        int sum=0, flag=0, cnt=0;        for(int i=0; i<n; i++) {            d[i]=IN.nextInt();            if(d[i]==0 || d[i]>n-1) flag=1;            if(d[i]==-1) continue;            sum+=d[i]-1;            cnt++;        }        IN.close();        if(n==1) {            if(d[0]==0 || d[0]==-1) System.out.println(1);            else System.out.println(0);            return;        }        if(n==2) {            if((d[0]==-1 || d[0]==1) && (d[1]==-1 || d[1]==-1)) System.out.println(1);            else System.out.println(0);            return;        }        if(flag==1) System.out.println(0);        p[0]=BigInteger.ONE;        for(int i=1; i<=n; i++) p[i]=p[i-1].multiply(BigInteger.valueOf(i));        ans=p[n-2].divide(p[n-2-sum]);        for(int i=0; i<n; i++) {            if(d[i]==-1) continue;            ans=ans.divide(p[d[i]-1]);        }        for(int i=0; i<n-2-sum; i++) ans=ans.multiply(BigInteger.valueOf(n-cnt));         System.out.println(ans);    }}

 

BZOJ 1005: [HNOI2008]明明的煩惱 Purfer序列 大數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.