1 条题解

  • 0
    @ 2025-8-24 21:58:40

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar GhostCai
    **

    搬运于2025-08-24 21:58:40,当前版本为作者最后更新于2018-06-04 17:55:29,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    f[i]表示深度不超过i的严格n元树的个数。
    转移时考虑加一个根,由于是n元树,根下需要n个子树
    f[i]=(f[i-1]^n)+1 答案即为对f[d]-f[d-1] 正体现了差分与前缀和的神奇作用

    边界f[0]=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[17];
    int n,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;
    }
    
    • 1

    信息

    ID
    3265
    时间
    1000ms
    内存
    125MiB
    难度
    6
    标签
    递交数
    0
    已通过
    0
    上传者