• BZOJ1089: [SCOI2003]严格n元树


    BZOJ1089: [SCOI2003]严格n元树

    dp+快速幂+高精度(这里用了java)

    f[i] 表示深度小于等于i的严格n元树的种类数

    ans = f[d] - f[d-1]

    f[0] = 1 

    f[i] = f[i-1]+ 1

    转移式考虑:

    对于深度为i-1的严格n元树,我们考虑将它加在一个的新的根节点下,构成一颗深度大于等于1,小于等于i的严格n元树。

    因为有n元,有n个分支,所以为n次方。再加上深度为0的情况,即只有一个根即可。

     

    /**************************************************************
        Problem: 1089
        User: solvit
        Language: Java
        Result: Accepted
        Time:888 ms
        Memory:17856 kb
    ****************************************************************/
     
    //package acm;
     
    import java.math.BigInteger;
    import java.awt.Container;
    import java.math.*;
    import java.math.BigInteger;
    import java.util.*;
     
    import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID; 
    public class Main
    {    
        public static BigInteger _pow(BigInteger x,int n)
        {
            BigInteger ret = BigInteger.ONE;
            while(n != 0)
            {
                if(n % 2 == 1)
                {
                    ret = ret.multiply(x);
                }
                x = x.multiply(x);
                n /= 2;
            }
            return ret;
        }
         
        public static void main(String[] args) 
        {            
            Scanner cin=new Scanner(System.in);
            int n = cin.nextInt();
            int d = cin.nextInt();
                
            if(d == 0){
                System.out.println(1);
            }
            else if(n == 0){
                System.out.println(1);
            }
            else {
               BigInteger f[] = new BigInteger[40];
               f[0] = BigInteger.ONE;
               for(int i = 1; i <= d; i++)
               {
                   f[i] = _pow(f[i-1], n);
                   f[i] = f[i].add(BigInteger.ONE);
               }
               System.out.println(f[d].subtract(f[d-1]));
            }
                
            cin.close();
          }
    }
    View Code
  • 相关阅读:
    LeetCode208 实现 Trie (前缀树)
    LeetCode289 生命游戏(模拟)
    LeetCode560 和为 K 的子数组
    LeetCode673 最长递增子序列的个数
    LeetCode31 下一个排列
    LeetCode926 将字符串翻转到单调递增
    LeetCode135 分发糖果
    LeetCode810 黑板异或游戏
    npm发布和修改详细教程
    不同的子序列问题I
  • 原文地址:https://www.cnblogs.com/solvit/p/9501172.html
Copyright © 2020-2023  润新知