设f(n)为所求答案
g(n)为n个顶点的非联通图
则f(n) + g(n) = h(n) = 2^(n * (n - 1) / 2)
其中h(n)是n个顶点的联图的个数
这样计算
先考虑1所在的连通分量包含哪些顶点
假设该连通分量有k个顶点
就有C(n - 1, k - 1)种集合
确定点集后,所在的连通分量有f(k)种情况。其他连通分量有 h(n - k)种情况
因此有递推公式。g(n) = sum{ C(n - 1, k - 1) * f(k) * h(n - k)} 其中k = 1,2...n-1
注意每次计算出g(n)后立刻算出f(n)
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger two[] = new BigInteger [55];
two[0] = BigInteger.ONE;
for(int i = 1; i <= 50; i++)
two[i] = two[i - 1].multiply(BigInteger.valueOf(2));
BigInteger h[] = new BigInteger [55];
for(int i = 1; i <= 50; i++){
int a = i;
int b = i - 1;
if(a % 2 == 0) a/= 2;
else b /= 2;
h[i] = BigInteger.ONE;
for(int j = 0; j < a; j++) {
h[i] = h[i].multiply(two[b]);
}
}
BigInteger C[][] = new BigInte