标签:
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.
4
41
答案 = 所有的情况 - 一个独立的情况 - 两个独立的情况 - .....
D(n) = 2 ^ (n * (n - 1) / 2) - 1 - sum{C(n, k) * dp[n - k] (1 <= k <= n - 1)}
package ds; /* *Author : 2486 *Memory: 32300 KB Time: 284 MS *Language: Java Result: Accepted *Public: */ import java.util.*; import java.math.*; import java.io.*; public class Main{ static final int MAXN = 100 + 5; static final BigInteger Two = new BigInteger("2"); static BigInteger [][] C = null; static BigInteger [] dp = null; static void Init(){ C = new BigInteger[MAXN][MAXN]; C[0][0] = BigInteger.ONE; for(int i = 1;i < MAXN ;i ++){ C[i][0] = BigInteger.ONE; C[i][i] = BigInteger.ONE; for(int j = 1;j < i;j ++){ C[i][j] = C[i - 1][j - 1].add(C[i - 1][j]); } } dp = new BigInteger[MAXN]; dp[1] = new BigInteger("1"); dp[2] = new BigInteger("1"); for(int i = 3;i < MAXN;i ++){ dp[i] = Two.pow(i * (i - 1) / 2).subtract(BigInteger.ONE); for(int k = i - 1;k >= 2;k --){ dp[i] = dp[i].subtract(dp[k].multiply(C[i][i - k])); } } } public static void main(String [] agrv) throws IOException { //System.setIn(new FileInputStream(new File("D:" + File.separator + "imput.txt"))); Scanner cin = new Scanner(System.in); Init(); while(cin.hasNext()){ int n = cin.nextInt(); System.out.println(dp[n]); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
ACdream 1420 High Speed Trains(容斥原理)
标签:
原文地址:http://blog.csdn.net/qq_18661257/article/details/48006199