Uva 10253 - Series-Parallel Networks 解题报告(递推)

96 篇文章 0 订阅

Problem H

Series-Parallel Networks

Input: standard input

Output:  standard output

Time Limit: 5 seconds

Memory Limit: 32 MB

 

In this problem you are expected to count two-terminal series-parallel networks. These are electric networks considered topologically or geometrically, that is, without the electrical properties of the elements connected. One of the two terminals can be considered as the source and the other as the sink.

 

A two-terminal network will be considered series-parallel if it can be obtained iteratively in the following way:

 

q       A single edge is two-terminal series-parallel.

q       If G1 and G2 are two-terminal series-parallel, so is the network obtained by identifying the sources and sinks, respectively (parallel composition).

q       If G1 and G2 are two-terminal series-parallel, so is the network obtained by identifying the sink ofG1 with the source of G2 (series composition).

 

Note here that in a series-parallel network two nodes can be connected by multiple edges. Moreover, networks are regarded as equivalent, not only topologically, but also when interchange of elements in series brings them into congruence; otherwise stated, series interchange is an equivalence operation. For example, the following three networks are equivalent:

 

 

Similarly, parallel interchange is also an equivalence operation. For example, the following three networks are also equivalent:

 

 

Now, given a number N, you are expected to count the number of two-terminal series parallel networks containing exactly N edges. For example, for N = 4, there are exactly 10 series-parallel networks as shown below:

 

 

Input

Each line of the input file contains an integer N (1 £ N £ 30) specifying the number of edges in the network.

A line containing a zero for N terminates the input and this input need not be considered.

 

Output

For each N in the input file print a line containing the number of two-terminal series-parallel networks that can be obtained using exactly N edges.

 

Sample Input

1

4

15

0

 

Sample Output

1

10

1399068



    解题报告:比较难搞的递推。主要是要能想到递推方程。不多说了,不懂就看书吧。(训练指南,p117)。

    代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long LL;
LL dp[100][100];

LL C(LL x,int y)
{
    double tmp=0;
    for(int i=1;i<=y;i++)
        tmp+=log10(x-i+1)-log10(i);
    return (LL)(pow(10,tmp)+0.5);
}

LL dfs(int x,int y)
{
    if(y==0) return 0;
    if(x==1||y==1||x==0) return 1;

    LL &ans=dp[x][y];
    if(~ans) return ans;
    ans=0;
    for(int i=0;i*y<=x;i++)
        ans+=dfs(x-i*y,y-1)*C(dfs(y,y-1)+i-1,i);
    return ans;
}

int main()
{
    memset(dp,-1,sizeof(dp));

    int n;
    while(~scanf("%d",&n) && n)
        printf("%lld\n", n==1?1:dfs(n,n-1)*2);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值