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判圈 ...
随机推荐
- 科学计算库Numpy——numpy.ndarray
创建ndarray 元素类型 对于ndarray结构来说,里面所有的元素必须是同一类型的,如果不是的话,会自动的向下进行转换. 元素类型所占字节数 数组维数 元素个数 数组的维度 数组中填充固定值 索 ...
- Huawei warns against 'Berlin Wall' in digital world
From China Daily Huawei technologies criticized recent registration imposed on the Chinese tech comp ...
- leetcode-17-BST
530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...
- python基础之文件处理总结
读文件: with open('contacts.txt', 'r', encoding='utf-8') as f: data = f.read() 二进制模式读 使用场景:网络传输(视频.图片或进 ...
- nrf52裸机学习——GPIO操作
/** * @brief Function for writing a value to a GPIO pin. * * Note that the pin must be configured as ...
- 自动设置IP地址bat脚本
自动获取IP及DNS: netsh interface ip set address name="本地连接" source=dhcpnetsh interface ip set d ...
- debian右键添加在终端中打开
sudo apt-get install nautilus-open-terminal -y 注销,重启
- joyoi tyvj1313 [NOIP2010初赛]烽火传递
单调队列优化dp #include <iostream> #include <cstdio> using namespace std; int dp[1000005], n, ...
- 大数据学习——sparkSql
官网http://spark.apache.org/docs/1.6.2/sql-programming-guide.html val sc: SparkContext // An existing ...
- MySQL 表数据的导入导出
数据导出 1. 使用 SELECT ...INTO OUTFILE ...命令来导出数据,具体语法如下. mysql> SELECT * FROM tablename INTO OUTFILE ...