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]n + 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(); } }