zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    不通过App Store,在iOS设备上直接安装应用程序(转)
    Quest Central for DataBase 5.0.1,6.1 (软件+注册)
    微信公众号自定义菜单与关键词自动回复互斥解决
    转载《Spring AOP中pointcut expression表达式解析 及匹配多个条件》
    ssm 动态切换数据源
    spring aop
    微信公众号自动回复
    转载《spring定时任务轮询(spring Task)》
    N 秒打开一个新窗口
    java webservice maven spring Class Not Found Exception解决
  • 原文地址:https://www.cnblogs.com/HarryGuo2012/p/4748915.html
Copyright © 2011-2022 走看看