zoukankan      html  css  js  c++  java
  • UVA 10516 Another Counting Problem

    UVA_10516

        我们不妨设在n叉树时,不超过i层的树的种类为f[i],那么对于由i-1变到i时,我们新添的根节点要么所有子树都有,即共有f[i-1]^n种情况,要么一个子树也没有,即只有一种情况,于是我们就可以得到递推式f[i]=f[i-1]^n+1,最后输出f[d]-f[d-1]即可。

        此外,这个题目说最后结果保证200位以内,但不是所有题目范围之内的数据都是200位以内的,只不过测试数据里面没有罢了,因此我们干脆不要去预处理了,有些结果是相当庞大的。

    import java.math.BigInteger;
    import java.util.Scanner;

    public class Main {
    public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    for(;;)
    {
    int n = cin.nextInt();
    int d = cin.nextInt();
    if(n == 0 && d == 0)
    break;
    BigInteger[] f = new BigInteger[20];
    f[0] = new BigInteger("1");
    for(int i = 1; i <= d; i ++)
    {
    f[i] = new BigInteger("1");
    for(int j = 0; j < n; j ++)
    f[i] = f[i].multiply(f[i - 1]);
    f[i] = f[i].add(BigInteger.ONE);
    }
    if(d == 0)
    System.out.println(n + " " + d + " " + "1");
    else
    System.out.println(n + " " + d + " " + f[d].add(f[d - 1].negate()));
    }
    }
    }


  • 相关阅读:
    drf3
    字典的操作方法
    列表的操作方法
    字符串的操作方法
    while循环和基本运算符
    初识数据类型
    USDT相关
    带团队
    CentOS7更改时区及同步网络时间
    mac胡刷新dns
  • 原文地址:https://www.cnblogs.com/staginner/p/2290920.html
Copyright © 2011-2022 走看看