ACdream 1420 High Speed Trains(容斥原理)

High Speed Trains

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Submit Statistic Next
Problem

Problem Description

The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed by high speed trains Shinkansens going all around the country. Therefore he decided to build the system of high
speed trains in Flatland.

Each high speed train line will be bidirectional and connect exactly two different cities of Flatland. Although there is actually no need of high speed trains in Flatland, the king ordered that there must be at
least one high speed train line from each city of Flatland.

The minister of transportation told the king that there are several train system satisfying his requirements. The king was amazed by the fact and asked the minister to count the number of possible systems.

Help the minister to calculate the number of train systems.

Input

The input file contains one integer number n (2 ≤ n ≤ 100)

Output

Output one integer number — the number of different train systems that can be arranged in Flatland.

Sample Input

4

Sample Output

41

答案 = 所有的情况 - 一个独立的情况 - 两个独立的情况 - .....

D(n) = 2 ^ (n * (n - 1) / 2) - 1 - sum{C(n, k) * dp[n - k] (1 <= k <= n - 1)}

package ds;
/*
 *Author : 2486
 *Memory: 32300 KB		Time: 284 MS
 *Language: Java		Result: Accepted
 *Public:
*/
import java.util.*;
import java.math.*;
import java.io.*;

public class Main{
	static final int MAXN = 100 + 5;
	static final BigInteger Two = new BigInteger("2");
	static BigInteger [][] C = null;
	static BigInteger [] dp = null;
	static void Init(){
		C = new BigInteger[MAXN][MAXN];
		C[0][0] = BigInteger.ONE;
		for(int i = 1;i < MAXN ;i ++){
			C[i][0] = BigInteger.ONE;
			C[i][i] = BigInteger.ONE;
			for(int j = 1;j < i;j ++){
				C[i][j] = C[i - 1][j - 1].add(C[i - 1][j]);
			}
		}
		dp = new BigInteger[MAXN];
		dp[1] = new BigInteger("1");
		dp[2] = new BigInteger("1");
		for(int i = 3;i < MAXN;i ++){
			dp[i] = Two.pow(i * (i - 1) / 2).subtract(BigInteger.ONE);
			for(int k = i - 1;k >= 2;k --){
				dp[i] = dp[i].subtract(dp[k].multiply(C[i][i - k]));
			}
		}
	}
	public static void main(String [] agrv)
	throws IOException
	{
		//System.setIn(new FileInputStream(new File("D:" + File.separator + "imput.txt")));
		Scanner cin = new Scanner(System.in);
		Init();
		while(cin.hasNext()){
			int n = cin.nextInt();
			System.out.println(dp[n]);
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 05:58:34

ACdream 1420 High Speed Trains(容斥原理)的相关文章

Acdream 1420 High Speed Trains(大数 + 容斥原理)

传送门 High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed

ACdream 1420 High Speed Trains【Java大数高精度 + 递推】

High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1420 Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was a

Gym 100338H High Speed Trains(高精度)

Gym 100338H High Speed Trains 题意: 求n个城市相互连通的方案数. 思路: 和HDU 4390迷之相似. 一共有n个城市,那么就有n(n?1)/2条边,每条边均有两种可能,选或不选.那么我们用ans[n]来表示n个城市相互连通的方案数: ans[n]=2n(n?1)/2?C1n?ans[n?1]?C2n?ans[n?2]?...?Cn?2n?ans[2]?C0n?ans[0] 答案 = 所有 - 一个城市独立的情况 - 两个城市独立的情况 - - - n-2个城市独

codeforces Gym 100338H High Speed Trains

递推就好了,用二项式定理算出所有连边的方案数,减去不合法的方案, 每次选出一个孤立点,那么对应方案数就是上次的答案. 枚举选几个孤立点和选哪些,选到n-1个点的时候相当于都不选,只减1. 要用到高精度,直接开100*100的组合数数组会MLE,用滚动数组优化一下就好了. 不会java,python太伤了 #include<bits/stdc++.h> using namespace std; const int MAXN = 20000; struct bign { int len, s[MA

递推 ACdream1420 High Speed Trains

传送门:点击打开链接 题意:n个点,每个点至少被一条边覆盖,不存在自环和重边.问有多少种情况 一个非常有意思的递推,其实可以利用容斥的思想去考虑这道题目 设F[n]表示为n个点的情况数,那么最多会存在n*(n-1)/2条边,这些边中,每条边都是可有可无.如果不考虑不满足条件的情况(也就是有的点没有被边覆盖的情况),可能的情况数为pow(2,n*(n-1)/2),也就是考虑每一条边是否存在 那么对于答案而言,不能有点没有被覆盖,接下来我们就来枚举点被边覆盖的个数,设为k 当有k个点被覆盖了,这k个

A Spy in the Metro

Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro con

UVa 1025 A Spy in the Metro(动态规划)

传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro

UVA1025---A Spy in the Metro(DP)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. Afterseveral thrilling events we ?nd her in the ?rst station of Algorithms City Metro, exam

UVA 1025 A Spy in the Metro DP

DP[ i ][ j ] 在 i 时刻 j 号车站的等待最小时间..... 有3种可能: 在原地等,坐开往左边的车,做开往右边的车 A Spy in the Metro Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Secret agent Maria was sent to Algorithms City to carry out an es