HDU 2068 RPG的錯排

來源:互聯網
上載者:User
RPG的錯排

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5536 Accepted Submission(s): 2269

Problem Description今年暑假杭電ACM集訓隊第一次組成女生隊,其中有一隊叫RPG,但做為集訓隊成員之一的野駱駝竟然不知道RPG三個人具體是誰誰。RPG給他機會讓他猜猜,第一次猜:R是公主,P是草兒,G是月野兔;第二次猜:R是草兒,P是月野兔,G是公主;第三次猜:R是草兒,P是公主,G是月野兔;......可憐的野駱駝第六次終於把RPG分清楚了。由於RPG的帶動,做ACM的女生越來越多,我們的野駱駝想都知道她們,可現在有N多人,他要猜的次數可就多了,為了不為難野駱駝,女生們只要求他答對一半或以上就算過關,請問有多少組答案能使他順利過關。

Input輸入的資料裡有多個case,每個case包括一個n,代表有幾個女生,(n<=25), n = 0輸入結束。

Sample Input

120

Sample Output

11

 

利用錯排和組合公式

錯排:A[i]=(i-1)X(A[i-1]+A[i-2]);A[0]=0;A[1]=0;A[2]=2;

組合:C(m,n)=m!/(m-n)!/n!

import java.util.*;import java.io.*;import java.math.BigInteger;public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(new BufferedInputStream(System.in));        while (sc.hasNextInt()) {            int n = sc.nextInt();            int m=0;            BigInteger sum=BigInteger.ONE;            if (n == 0)                System.exit(0);                                    for(int i=0;i<=n/2;i++){                sum=sum.add(zhuHe(n).divide(zhuHe(n-i)).divide(zhuHe(i)).multiply(cuoPai(i)));                            }            System.out.println(sum);        }    }    public static BigInteger zhuHe(int m){        BigInteger big[]=new BigInteger[25+1];        big[0]=BigInteger.ONE;        big[1]=BigInteger.ONE;        for(int i=1;i<=m;i++){            big[i]=big[i-1].multiply(BigInteger.valueOf(i));        }        return big[m];    }    public static BigInteger cuoPai(int m){        BigInteger big[]=new BigInteger[26];        big[0]=BigInteger.ZERO;        big[1]=BigInteger.ZERO;        big[2]=BigInteger.valueOf(1);        for(int i=3;i<=m;i++){            big[i]=BigInteger.valueOf((i-1)).multiply(big[i-1].add(big[i-2]));        }        return big[m];    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.