【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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值