UVA10516 Another Counting Problem大数+递推

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10516 Another Counting Problem大数+递推相关的知识,希望对你有一定的参考价值。

Tree is an important data structure in Computer Science. Of all trees we work with, Binary Tree is probably the most popular one. A Binary Tree is called Strictly Binary Tree if every nonleaf node in a binary tree has nonempty left and right subtrees. Let us define a Strictly Binary Tree of depth d, as a Strictly Binary Tree that has at least one root to leaf path of length d, and no root to leaf path in that tree is longer than d. So let us use a similar reasoning to define a generalized structure.
 A n-ary Tree is called Strictly n-ary Tree if every nonleaf node in a n-ary tree has n children each. A Strictly n-ary Tree of depth d, then can be defined as a Strictly n-ary Tree that has at least one root to leaf path of length d, and no root to leaf path in that tree is longer than d.
 Given the value of n and depth d, your task is to find the number of different strictly n-ary trees of depth d.
 The figure below shows the 3 different strictly binary trees of depth 2.

Input
Input consists of several test cases. Each test case consists of two integers n (0 < n ≤ 32), d (0 ≤ d ≤ 16). Input is terminated a test case where n = 0 and d = 0, you must not process this test case.
Output
For each test case, print three integers, n, d and the number of different strictly n-ary trees of level d, in a single line. There will be a single space in between two integers of a line. You can assume that you would not be asked about cases where you had to consider trees that may have more than 210 nodes in a level of the tree. You may also find it useful to know that the answer for each test case will always fit in a 200 digit integer.
Sample Input
2 0
2 1
2 2
2 3
3 5
0 0
Sample Output
2 0 1
2 1 1
2 2 3
2 3 21
3 5 58871587162270592645034001

问题链接UVA10516 Another Counting Problem
问题简述:(略)
问题分析:定义n叉树不超过i层的树种类为f[i],那么得递推式f[i]=f[i-1]^n+1。最后输出结果f[d]-f[d-1]。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的Java语言程序如下:

/* UVA10516 Another Counting Problem */

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[16 + 1];
            f[0] = new BigInteger("1");
            for (int i = 1; i <= d; i++) 
                f[i] = new BigInteger("1");
                for (int j = 1; 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()));
        
    

以上是关于UVA10516 Another Counting Problem大数+递推的主要内容,如果未能解决你的问题,请参考以下文章

C. Yet Another Counting Problem(循环节规律)

CF1748E Yet Another Array Counting Problem

Educational Codeforces Round 86 (Rated for Div. 2) C—Yet Another Counting Problem

UVa 1225 Digit Counting --- 水题

UVA10502 Counting Rectangles水题

Digit Counting UVA - 1225