原题链接: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. Thinkphp 5 调试执行的SQL语句

    在模型操作中 ,为了更好的查明错误,经常需要查看下最近使用的SQL语句,我们可以用getLastsql方法来输出上次执行的sql语句.例如: User::get(1); echo User::getL ...

  2. python 数据结构与算法之排序(冒泡,选择,插入)

    目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...

  3. redis的字符串操作以及在django中的使用

    redis ----redis.MongoDB : 非关系型数据库 redis   存储在内存中 MongoDB 存储在硬盘中 l  简介 redis是一个key-value存储系统 , 支持持久化 ...

  4. PreResultListener

    PreResultListener是要给监听器接口,他可以在Action完成控制处理之后,系统转入实际的物理视图之间被回调. Struts 2 可以由ActionInvocation的addPreRe ...

  5. PHP杂技(一)

    逻辑运算符 &&和& ||和|的部分区别 返回结果类型不同, A||B 如果A为真那么B不会做判断,而A|B前后都做判断 switch判断中并不是===,更像是==,例如(1) ...

  6. java异常处理及400,404,500错误处理

        java代码中经常碰到各种需要处理异常的时候,比如什么IOException  SQLException  NullPointException等等,在开发web项目中,遇到异常,我现在做的就 ...

  7. iOS-----5分钟学会枚举的正确使用姿势-Enumeration宏

    前言 Enum,枚举,相信大部分编程语言都有对应的枚举类型,功能可能有多有少,但是枚举最核心的功能是 “规范的定义代码中的状态.状态码.选项”. 状态.状态码.选项 什么是状态:同时只能出现一个值(状 ...

  8. ubuntu安装jdk<服务器>

    服务器 阿里云服务器Ubuntu安装jdk7 2014-08-25 16:44 |  coding云 |  5825次阅读 | 6条评论   一.下载jdk 可以先下载到本地,然后ftp到服务器 官方 ...

  9. Json操作(汇总)

    利用:com.fasterxml.jackson 原文地址:https://blog.csdn.net/joyous/article/details/9448461 说明:Map转化为Json:创建J ...

  10. [CQOI2010] 扑克牌 (二分答案,巧解)

    Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1套 ...