POJ1737 Connected Graph【数学】
来源:互联网 发布:上古卷轴mod知乎 编辑:程序博客网 时间:2024/06/05 18:18
题意:有n个点,问把它们连起来的方案数
思路:n个点一共n * (n-1) / 2 条边,设为h[n],一共方案数2 ^ h[n],去掉不连通的情况,选取一个固定点,再选k(0 ≤ k < n-1)个点方案数C(n-1,k),和它连通方案数ans[k+1],其他n-1-k个点的条边随便连
ans[n] = 2^h[n]- sum{ C(n-1,k) * ans[k+1] * 2^h[ n-1-k ] } (0 ≤ k < n-1)
import java.util.*;import java.math.*;public class Main{static BigInteger ans[] = new BigInteger[55];static int h[] = new int[55];static BigInteger C[][] = new BigInteger[55][55];static BigInteger two[] = new BigInteger[2505];public static void main(String[] args) {// TODO Auto-generated method stubtwo[0] = BigInteger.ONE;for(int i = 1; i < 2505; i++)two[i] = two[i-1].multiply(BigInteger.valueOf(2));C[0][0] = BigInteger.ONE;for(int i = 1; i <= 50; i++){for(int j = 0; j <= i; j++){if(j == 0 || j == i) C[i][j] = BigInteger.ONE;else C[i][j] = C[i-1][j-1].add( C[i-1][j] );}}h[1] = 0;for(int i = 2; i <= 50; i++)h[i] = i*(i-1)/2;ans[1] = BigInteger.ONE;for(int i = 2; i <= 50; i++){BigInteger temp;temp = two[ h[i] ];for(int j = 0; j <= i-2; j++)temp = temp.subtract(two[h[i-1-j]].multiply(ans[j+1]).multiply(C[i-1][j]));ans[i] = temp;}Scanner sc = new Scanner(System.in);while(sc.hasNext()){int n = sc.nextInt();if(n == 0) break;System.out.println(ans[n]);}}}/*a.add(b); 加法运算 a.subtract(b); 减法运算a.multiply(b); 乘法运算 a.divide(b); 除法运算a.mod(b); 取模运算a.gcd(b); 最大公因数 a.max(b); 最大值(a,b) a.min(b); 最小值(a,b) a.modPow(k, mod); 返回(a^k)%mod a.pow(b);a.toString(); 返回大整数的string型System.out.println(a); 自带换行的输出 System.out.print(a); 不自带换行的输出Scanner sc = new Scanner(System.in); if(n.compareTo(BigInteger.ZERO)==0) break; */
第二次写Java,temp.subtract()完了后,竟然忘记赋给temp了,上次是add(),调了半天,果然还是不习惯
阅读全文
0 0
- POJ1737 Connected Graph【数学】
- poj1737 Connected Graph(组合数学)
- poj1737 Connected Graph
- Connected Graph POJ1737 高精度
- Connected Graph
- poj1737
- poj 1737 Connected Graph
- poj 1737 Connected Graph
- Nodally 3-connected graph
- POJ 1737 Connected Graph
- POJ 1737 Connected Graph
- Connected Component in Undirected Graph
- sicily connected components in undiected graph
- Sicily 1002. connected components in undirected graph
- POJ 1737 Connected Graph 递推
- POJ 1737 Connected Graph 题解(未完成)
- LintCode 431 Connected Component in Undirected Graph
- POJ 1737 Connected Graph 高进度+递推
- TCP 简单理解
- 文件编程----------------open(),read()
- [随心译]2017.8.6-增强与虚拟现实的消费每年都在翻倍
- rxjs -- 响应式编程
- 成为IT精英,我奋斗了七年
- POJ1737 Connected Graph【数学】
- 【Angular2】手风琴样式页面制作
- [LeetCode]645. Set Mismatch
- Rank of Tetris HDU
- laravel5.4生成验证码
- 经纬度算两点距离
- 第十七章 优于select的epoll
- 完美世界hr给大家的简历修改和面试的建议
- 设计模式