Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度
原题链接: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+高精度的更多相关文章
- codeforces Gym 100338H High Speed Trains (递推,高精度)
递推就好了,用二项式定理算出所有连边的方案数,减去不合法的方案, 每次选出一个孤立点,那么对应方案数就是上次的答案. 枚举选几个孤立点和选哪些,选到n-1个点的时候相当于都不选,只减1. 要用到高精度 ...
- Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度
Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1003 ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- codeforces 869C The Intriguing Obsession【组合数学+dp+第二类斯特林公式】
C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- Codeforces #144 (Div. 1) B. Table (组合数学+dp)
题目链接: B.Table 题意: \(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法. \((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ ...
- 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 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
随机推荐
- matplotlib学习记录 一
from matplotlib import pyplot as plt # 先实例一个图片,传入图片参数,10宽,5高,分辨率为80 image = plt.figure(figsize=(10,5 ...
- shutil,zipfile,tarfile模块
一,shutil模块 1.shutil.chown() shutil.chown('test.txt',user='mysql',group='mysql') #改变文件的属主和属组. 2.shuti ...
- 转载:使用Pandas进行数据匹配
使用Pandas进行数据匹配 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas进行数据匹配 目录 merge()介绍 inner模式匹配 lefg模式匹配 right模式匹配 outer模式 ...
- leetcode-15-basic-string
58. Length of Last Word 解题思路: 从结尾向前搜索,空格之前的就是最后一个词了.写的时候我考虑了尾部有空格的情况.需要注意的是,测试用例中有" "的情况,此 ...
- exp分析
1 from pwn import* 2 3 local =1 4 debug = 1 5 6 if local: 7 p = process('./pwn1') 8 else: 9 p = remo ...
- POJ:2955-Brackets(经典:括号匹配)
传送门:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS Memory Limit: 65536K Description We g ...
- hdu 5984
PockyTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissio ...
- BZOJ 4896: [Thu Summer Camp2016]补退选
trie树+vector+二分 别忘了abs(ans) #include<cstdio> #include<algorithm> #include<vector> ...
- Java学习笔记2---设置环境变量JAVA_HOME,CLASSPATH,PATH
1.环境变量包括: JAVA_HOME,CLASSPATH,PATH 2.设置环境变量的目的: 路径搜索,方便查找到jdk的安装路径.方便搜索用到的类文件.方便搜索用到的可执行文件如java,java ...
- 触屏版轻量级分页插件jqPagination分享
说到HTML5和jquery上的分页问题,优秀的分页插件网上一抓一大把,然而同时适合兼容在Ipad和手机端的网站分页却不是特别多. 或许有人会说,触屏现在流行下拉底部后加载下一页内容,类似微博和QQ空 ...