您的位置:首页 > 其它

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

2014-03-27 18:51 323 查看
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数学 DP counting