Time Limit: 1 Sec Memory Limit: 162 MB
Submit: 2123 Solved: 1065
[Submit][Status][Discuss]
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 3
【樣例輸入3】
3 5
Sample Output
【樣例輸出1】
3
【樣例輸出2】
21
【樣例輸出2】
58871587162270592645034001
HINT
Source
考慮f[i]表示深度不超過i的嚴格n元樹的個數。
這樣考慮的好處是,如果定義爲恰好深度i的個數,那麼很難轉移,因爲它不滿,可能有一棵子樹使其達到i層,其他不滿,就很難受。
但是定義成這種前綴和形式,就可以轉移了,把這種情況全考慮進去,最後差分即可。
巧妙,歎爲觀止。
轉移時考慮加一個根,由於是n元樹,根下需要n個子樹
f[i]=(f[i-1]^n)+1
需要高精度(並不能默寫。。)
以及當d=0時,特判答案1。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 10000;
struct bign{
int d[maxn], len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bign() { memset(d, 0, sizeof(d)); len = 1; }
bign(int num) { *this = num; }
bign(char* num) { *this = num; }
bign operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bign operator = (int num){
char s[2000]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator + (const bign& b){
bign c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bign operator - (const bign& b){
bign c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bign operator * (const bign& b)const{
int i, j; bign c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bign operator / (const bign& b){
int i, j;
bign c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bign operator % (const bign& b){
int i, j;
bign a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bign operator += (const bign& b){
*this = *this + b;
return *this;
}
bool operator <(const bign& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bign& b) const{return b < *this;}
bool operator<=(const bign& b) const{return !(b < *this);}
bool operator>=(const bign& b) const{return !(*this < b);}
bool operator!=(const bign& b) const{return b < *this || *this < b;}
bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
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 f[35];
int n;
int d;
int main(){
cin>>n>>d;
if(n==1&&d==1) return cout<<0,0;
if(d==0) return cout<<1,0;
f[1]=1;
for(int i=1;i<=d;i++) {
bign tmp=1;
for(int j=1;j<=n;j++) tmp=tmp*f[i-1];
f[i]=f[i]+tmp+1;
}
bign ans=f[d]-f[d-1];
cout<<ans;
}