UVA 10253Series-Parallel Networks

题意:给定n,求有n条边的串并联网络的方案数。

分析:《算法竞赛入门经典训练指南》数学基础例题7。书中给出的建模思想很好,并且对组合数学求解和dp的边界情况分析都很好。多学习,多积累。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int MAX=151;
const int MOD1=1000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
ll f[35],dp[35][35];
ll C(ll n,ll m) {
    db ans=1;
    for (int i=1;i<=m;i++) ans*=n-i+1;
    for (int i=1;i<=m;i++) ans/=i;
    return (ll)(ans+0.5);
}
int main()
{
    int i,j,k,n=30;
    f[1]=1;
    memset(dp,0,sizeof(dp));
    for (i=0;i<=n;i++) dp[i][0]=1;
    for (i=1;i<=n;i++) { dp[0][i]=0;dp[i][1]=1; }
    for (i=1;i<=n;i++) {
        for (j=2;j<=n;j++) {
            dp[i][j]=0;
            for (k=0;k*i<=j;k++)
            dp[i][j]+=C(f[i]+k-1,k)*dp[i-1][j-k*i];
        }
        f[i+1]=dp[i][i+1];
    }
    while (scanf("%d", &n)&&n)
    printf("%lld\n", n==1 ? 1:2*f[n]);
    return 0;
}


你可能感兴趣的:(UVA 10253Series-Parallel Networks)