High Speed Trains
Problem 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
Output
Sample Input
4
Sample Output
41
题意:
分析:
代码实现:
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]);
}
}
}