原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-andrew-stankevich-contest-22-asc-22-en.pdf

题意

给你n个点,让你连边,使得每个点的度至少为1,问你方案数。

题解

从正面考虑非常困难,应从反面考虑,取 i 点出来不连,这样的取法一共有C(n,i)种取法,其他的连好,而这样就是子问题了。那么dp[n]=2^(n*(n-1)/2)-sum(C(n,i)*dp[n-i])-1,2<=i<=n-1

代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*; public class Main {
static int n;
static BigInteger dp[] = new BigInteger[110];
static BigInteger c[][] = new BigInteger[110][110];
static BigInteger ps[] = new BigInteger[10010];
static BigInteger x;
public Main() throws FileNotFoundException{
Scanner cin = new Scanner(new File("trains.in"));
PrintWriter cout = new PrintWriter(new File("trains.out"));
n = cin.nextInt();
//System.out.println("n = " + n);
for(int i=1;i<=100;i++){
c[i][0] = new BigInteger("1");
c[i][1] = new BigInteger(Integer.toString(i));
c[i][i] = new BigInteger("1");
}
for(int i=2;i<=100;i++){
for(int j=2;j<i;j++){
c[i][j] = c[i-1][j].add(c[i-1][j-1]);
}
}
ps[0] = new BigInteger("1");
for(int i=1;i<=10001;i++){
ps[i] = ps[i-1].multiply(new BigInteger("2"));
}
dp[2] = new BigInteger("1");
dp[3] = new BigInteger("4");
for(int i=4;i<=n;i++){
x = new BigInteger("0");
for(int j=2;j<=i-1;j++){
x = x.add(c[i][i-j].multiply(dp[j]));
}
x = x.add(new BigInteger("1"));
dp[i] = ps[i*(i-1)/2].subtract(x);
}
cout.println(dp[n]);
//System.out.println(dp[n]);
cin.close();
cout.close();
} public static void main(String[] args) throws FileNotFoundException {
new Main();
}
}

Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度的更多相关文章

  1. codeforces Gym 100338H High Speed Trains (递推,高精度)

    递推就好了,用二项式定理算出所有连边的方案数,减去不合法的方案, 每次选出一个孤立点,那么对应方案数就是上次的答案. 枚举选几个孤立点和选哪些,选到n-1个点的时候相当于都不选,只减1. 要用到高精度 ...

  2. Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

    Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1003 ...

  3. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  4. codeforces 869C The Intriguing Obsession【组合数学+dp+第二类斯特林公式】

    C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  6. Codeforces #144 (Div. 1) B. Table (组合数学+dp)

    题目链接: B.Table 题意: \(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法. \((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ ...

  7. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  8. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  9. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

随机推荐

  1. HDU:5040-Instrusive

    Instrusive Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Proble ...

  2. MySQL配置允许远程登录

    MySQL默认只允许用户本地登录,需要远程连接可进行如下操作: 允许root用户在任何地方进行远程登录,并具有所有库任何操作权限: 使用root用户登录mysql: mysql -u root -p& ...

  3. SPOJ 375 树链剖分 QTREE - Query on a tree

    人生第一道树链剖分的题目,其实树链剖分并不是特别难. 思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护. 貌似大家都是从这篇博客上学的. #include <c ...

  4. Scala学习-01-变量与类型

    Scala运行在jvm之上,可以调用Java类库和与Java框架交互,并将面向对象与面向函数结合在一起. 特点: 1 保留了静态类型检查.安全保障高. 2 函数式编程,更加灵活. 3 运行于jvm之上 ...

  5. Leetcode7--->Reverse Integer(逆转整数)

    题目: 给定一个整数,求将该整数逆转之后的值: 举例: Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 在这里只用 ...

  6. set的特性和基本用法——python3.6

    特性 无序,不重复的数据组合,用{}表示,eg:{1,2,3,4,5,6} 用途 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之间的交集,差集,并集,对称差集,包含(子集和超集,相交 ...

  7. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)

    这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  8. 《机器学习实战》笔记——AdaBoost

    笔记见备注 # _*_ coding:utf-8 _*_ from numpy import * # 简单数据集 def loadSimpData(): datMat = matrix([[1., 2 ...

  9. 2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest BHanoi tower

    B Hanoi tower It has become a good tradition to solve the “Hanoi tower” puzzle at programming contes ...

  10. POJ 2181 Jumping Cows

    Jumping Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6398   Accepted: 3828 Desc ...