1089: [SCOI2003]严格n元树

这题其实很水啊,就是高精度写了我一会。。

这个考虑对d值进行DP
我们只要限定有d-1这个深度就可以推过去了。。
然后因为怕重复,所以我枚举了一下最左边的d-1深度
算了,不想写了。。看看暴力代码吧。。有注释的

#include<cstdio>
#include<cstdlib>
#include<cstring>
struct Int
{
    int a[10000];//东西 
    int len;//位数
};
int n,d;
int f[20];
void dfs (int x)//我想得到深度为d的状况
{
    if (x==1)   {f[x]=1;return ;}
    dfs(x-1);
    f[x]=0;
    for (int u=1;u<=n;u++)//枚举第一个d-1层的是哪一个儿子
    {
        int tt=1;
        int sum=0;
        for (int j=1;j<=x-2;j++)//x-2层总共有多少种状况 
            sum=sum+f[j];
        for (int i=1;i<u;i++)//那么这些就都是x-2了 
            tt=tt*sum;
        tt=tt*f[x-1];
        sum=sum+f[x-1];
        for (int i=u+1;i<=n;i++)
            tt=tt*sum;
            if (x==5)
        {
            printf("%d %d\n",tt,sum);
        }
        f[x]+=tt;

    }
    return ;
}
int main()
{
    f[0]=0;
    scanf("%d%d",&n,&d);d++;
    dfs(d);
    printf("%d",f[d]);
    //printf(f[d]);
    return 0;
}

然后这题要用高精度。。好久没写,于是写挫了QAQ
反正数据小,很多地方都没有优化

#include<cstdio>
#include<cstdlib>
#include<cstring>
struct Int
{
    int a[10000];//东西 
    int len;//位数
    void clear() {memset(a,0,sizeof(a));len=1;}
};
int n,d;
Int f[20];
int mymax (int x,int y){return x>y?x:y;}
void print (Int x)
{
    for (int u=x.len;u>=1;u--)
        printf("%d",x.a[u]);
    printf("\n");
}
Int add (Int a,Int b)
{
    Int c;c.clear();
    int len=mymax(a.len,b.len);
    //print(a);print(b);
    //printf("%d\n",len);
    for (int u=1;u<=len;u++)
    {
        c.a[u]+=a.a[u]+b.a[u];
        c.a[u+1]+=c.a[u]/10;
        c.a[u]%=10;
    }
    c.len=len;
    while (c.a[c.len+1]>0)
    {
        c.len++;
        c.a[c.len+1]+=(c.a[c.len]/10);
        c.a[c.len]%=10;
    }
    while (c.a[c.len]==0) c.len--;
    return c;
}
Int Mul (Int a,Int b)
{
    Int c;c.clear();
    for (int u=1;u<=a.len;u++)
        for (int i=1;i<=b.len;i++)
            c.a[u+i-1]=c.a[u+i-1]+a.a[u]*b.a[i];
    c.len=0;
    while (c.a[c.len+1]>0||c.len<=a.len+b.len+5)
    {
        c.len++;
        c.a[c.len+1]=c.a[c.len+1]+(c.a[c.len]/10);
        c.a[c.len]%=10;
    }
    while (c.a[c.len]==0) c.len--;
    return c;
}
void dfs (int x)//我想得到深度为d的状况
{
    if (x==1)   
    {
        f[x].clear();
        f[x].a[1]=1;
        return ;
    }
    dfs(x-1);
    f[x].clear();
    for (int u=1;u<=n;u++)//枚举第一个d-1层的是哪一个儿子
    {
        Int tt;tt.clear();tt.a[1]=1;
        Int sum;sum.clear();
        for (int j=1;j<=x-2;j++)//x-2层总共有多少种状况 
            sum=add(sum,f[j]);
            //sum=sum+f[j];
        for (int i=1;i<u;i++)//那么这些就都是x-2了 
            tt=Mul(tt,sum);
            //tt=tt*sum;
        //tt=tt*f[x-1];
        tt=Mul(tt,f[x-1]);
        //sum=sum+f[x-1];
        sum=add(sum,f[x-1]);
        for (int i=u+1;i<=n;i++)
            tt=Mul(tt,sum);
        /*if (x==5)
        {
            print(tt);print(sum);
        }*/
            //tt=tt*sum;
        f[x]=add(f[x],tt);
        //f[x]+=tt;

    }
    //print(f[x]);
    return ;
}
int main()
{
    f[0].clear();
    scanf("%d%d",&n,&d);d++;
    dfs(d);
//  printf("%d",f[d]);
    print(f[d]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值