【BZOJ 1089】[SCOI2003]严格n元树

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

设fi表示深度为i的树个数,si是fi的前缀和,即si为深度不超过i树的个数。 那么si=s[i-1]^n + 1

就是说 先选一个节点作为根节点 然后选n个深度不超过i-1的树接在根节点下面。

这n个子树每个子树都有s[i-1]种取法。
所以是它的n次方。

注意:si这里混杂了深度为i和小于i的树。但没有深度为0的了,所以把这个深度为0的一个节点加上去就好.也即递推式中的加1

最后答案就是s[d]-s[d-1]了

用java的biginteger写
(加一个快速幂

【代码】

import java.math.BigInteger;
import java.util.*;
public class Main {
	
	private static BigInteger ksm(BigInteger x,int y) {
		BigInteger temp = new BigInteger("1");
		while (y>0) {
			if ((y&1)==1) temp = temp.multiply(x);
			x = x.multiply(x);
			y>>=1;
		}
		return temp;
	}
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n,d;
		n = cin.nextInt();d = cin.nextInt();
		BigInteger a = new BigInteger("1");
		for (int i = 1;i <= d;i++) {
			BigInteger b = ksm(a,n);
			b = b.add(new BigInteger("1"));
			if (i==d)
				a = b.subtract(a);
			else
				a = b;
		}
		System.out.println(a);
	}
}

原文地址:https://www.cnblogs.com/AWCXV/p/8573820.html