Description
如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树。如果该树中最底层的节点深度为d
(根的深度为0),那么我们称它为一棵深度为d的严格n元树。例如,深度为2的严格2元树有三个,如下图:
给出n, d,编程数出深度为d的n元树数目。
Input
仅包含两个整数n, d( 0 < n < = 32, 0 < = d < = 16)
Output
仅包含一个数,即深度为d的n元树的数目。
Sample Input
【样例输入1】
2 2
2 2
【样例输入2】
2 3
2 3
【样例输入3】
3 5
Sample Output
【样例输出1】
3
3
【样例输出2】
21
【样例输出2】
58871587162270592645034001
Solution
DP方程好想= =
$f[i]$表示深度不超过$i$的树的种数
$f[i]=f[i-1]^n+1$。加1是因为儿子可能为空。
最后答案为$f[d]-f[d-1]$
需要高精度……不过懒得写了直接套了个板子QAQ
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#define N (509)
using namespace std; struct bign
{
int len, s[N];
bign ()
{
memset(s, , sizeof(s));
len = ;
}
bign (int num) { *this = num; }
bign (const char *num) { *this = num; }
bign operator = (const int num)
{
char s[N];
sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator = (const char *num)
{
for(int i = ; num[i] == ''; num++) ; //去前导0
len = strlen(num);
for(int i = ; i < len; i++) s[i] = num[len-i-] - '';
return *this;
}
bign operator + (const bign &b) const //+
{
bign c;
c.len = ;
for(int i = , g = ; g || i < max(len, b.len); i++)
{
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % ;
g = x / ;
}
return c;
}
bign operator += (const bign &b)
{
*this = *this + b;
return *this;
}
void clean()
{
while(len > && !s[len-]) len--;
}
bign operator * (const bign &b) //*
{
bign c;
c.len = len + b.len;
for(int i = ; i < len; i++)
{
for(int j = ; j < b.len; j++)
{
c.s[i+j] += s[i] * b.s[j];
}
}
for(int i = ; i < c.len; i++)
{
c.s[i+] += c.s[i]/;
c.s[i] %= ;
}
c.clean();
return c;
}
bign operator *= (const bign &b)
{
*this = *this * b;
return *this;
}
bign operator - (const bign &b)
{
bign c;
c.len = ;
for(int i = , g = ; i < len; i++)
{
int x = s[i] - g;
if(i < b.len) x -= b.s[i];
if(x >= ) g = ;
else
{
g = ;
x += ;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
bign operator -= (const bign &b)
{
*this = *this - b;
return *this;
}
bign operator / (const bign &b)
{
bign c, f = ;
for(int i = len-; i >= ; i--)
{
f = f*;
f.s[] = s[i];
while(f >= b)
{
f -= b;
c.s[i]++;
}
}
c.len = len;
c.clean();
return c;
}
bign operator /= (const bign &b)
{
*this = *this / b;
return *this;
}
bign operator % (const bign &b)
{
bign r = *this / b;
r = *this - r*b;
return r;
}
bign operator %= (const bign &b)
{
*this = *this % b;
return *this;
}
bool operator < (const bign &b)
{
if(len != b.len) return len < b.len;
for(int i = len-; i >= ; i--)
{
if(s[i] != b.s[i]) return s[i] < b.s[i];
}
return false;
}
bool operator > (const bign &b)
{
if(len != b.len) return len > b.len;
for(int i = len-; i >= ; i--)
{
if(s[i] != b.s[i]) return s[i] > b.s[i];
}
return false;
}
bool operator == (const bign &b)
{
return !(*this > b) && !(*this < b);
}
bool operator != (const bign &b)
{
return !(*this == b);
}
bool operator <= (const bign &b)
{
return *this < b || *this == b;
}
bool operator >= (const bign &b)
{
return *this > b || *this == b;
}
string str() const
{
string res = "";
for(int i = ; i < len; i++) res = char(s[i]+'') + res;
return res;
}
}; istream& operator >> (istream &in, bign &x)
{
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator << (ostream &out, const bign &x)
{
out << x.str();
return out;
} bign Pow(bign a,int b)
{
bign ans=;
for (int i=; i<=b; ++i)
ans=ans*a;
return ans;
} bign f[],ans;
int n,d; int main()
{
scanf("%d%d",&n,&d);
if (!d){puts(""); return ;}
f[]=;
for (int i=; i<=d; ++i)
f[i]=Pow(f[i-],n)+;
cout<<f[d]-f[d-];
}