## 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

/**************************************************************
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);
}
System.out.println(f[d].subtract(f[d-1]));
}

cin.close();
}
}
View Code

posted on 2018-08-19 14:28  solvit  阅读(165)  评论(0编辑  收藏  举报