ACdream 1420 High Speed Trains【Java大數高精度 + 遞推】

來源:互聯網
上載者:User

標籤:大數高精度   java   

High Speed TrainsTime Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)連結:http://acdream.info/problem?pid=1420Problem Description

      The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed by high speed trains Shinkansens going all around the country. Therefore he decided to build the system of high speed trains in Flatland.

      Each high speed train line will be bidirectional and connect exactly two different cities of Flatland. Although there is actually no need of high speed trains in Flatland, the king ordered that there must be at least one high speed train line from each city of Flatland.
      The minister of transportation told the king that there are several train system satisfying his requirements. The king was amazed by the fact and asked the minister to count the number of possible systems.
      Help the minister to calculate the number of train systems.

Input      The input file contains one integer number n (2 ≤ n ≤ 100)Output      Output one integer number — the number of different train systems that can be arranged in Flatland.Sample Input
4
Sample Output
41

題意:給定N個點,保證每個點都至少有一條邊與它相連,問可能情況的方案數。2 <= N < = 100分析:設ans[i] 表示的是i個點保證每個點都至少有一條邊與它相連,可能情況的方案數。N個頂點,最多構成N *(N - 1)/ 2條邊,每條邊有兩種狀態,即 選 和 不選,那麼所有情況的狀態數是2^(N *(N - 1)/ 2) ,然後將總狀態數分別減去一個頂點無邊相連的情況(即ans[N-1] * C(N-1,N)),兩個頂點無邊相連的情況(即ans[N-2] * C(N-2,N)),.......,N - 2個頂點無邊相連的情況(即ans[2]*C(2,N)),N - 1個頂點無邊相連的情況(這種情況特判,為0,因為,不存在僅有一個頂點存在邊的情況),N 個頂點無邊相連的情況 (即 ans[0]*C(0,N) == 1)。這樣,ans[i] 就可以由ans[j]  (j ∈ [0,N-1] )遞推求解出來了。最後,大數的處理,Java的 BigInteger類 用起來真是方便啊!代碼實現:
import java.io.*;import java.util.*;import java.math.*;public class Main {public static BigInteger C(int k,int n){BigInteger ret = BigInteger.valueOf(1);for(int i = n;i >= n-k+1;i--) ret = ret.multiply(BigInteger.valueOf(i));for(int i = 2;i <= k;i++) ret = ret.divide(BigInteger.valueOf(i));return ret;}public static void main(String[] args) {BigInteger ONE = BigInteger.valueOf(1);BigInteger TWO = BigInteger.valueOf(2);BigInteger ans[] = new BigInteger[105];ans[2] = BigInteger.valueOf(1);for(int n = 3;n <= 100;n++) {int ENum = n*(n-1)/2;ans[n] = TWO.pow(ENum).subtract(ONE);for(int i = 2;i < n;i++){ans[n] = ans[n].subtract(C(i, n).multiply(ans[i]));}}Scanner cin = new Scanner(System.in);while(cin.hasNext()) {int N = cin.nextInt();System.out.println(ans[N]);}}}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

ACdream 1420 High Speed Trains【Java大數高精度 + 遞推】

聯繫我們

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