【CF Gym 100637K】 Microcircuits 【DP】【环上n个点,问不相交的连k条线的方案数】

传送门:

描述:

K. Microcircuits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You probably know how microcircuits look like. First of all it is important to pay special attention to connections. Contacts on the circuit are connected by lines. If two lines do not intersect then there is no connection between respective contacts.

You are holding a circular circuit with n contacts around the borderline. You have to calculate the number of possibilities to put exactly knon-intersecting lines each connecting two contacts.

Input

Single line contains two integers n and k (1 ≤ k ≤ n ≤ 40).

Output

Output the required number.

Examples
input
4 2
output
2
input
4 3
output
0

题意:

给你n个点,将这些点放在一个环上,问你不相交的连k条线的方案数。(没有重点)

思路:

dp[i][j]表示i个点连j条线的方案数,那么新加一个点i,

情况1:i没有和之前的点相连,方案数为dp[i-1][j];

情况2:i和p号点相连(0<p<j),那么就i-p这条线把所有点划分成两部分,然后枚举一些子问题的连边数,方案数为sum( dp[p-1][q]*dp[i-1-p][j-1-q] ) (0<=q<j)

复杂度:

O(n^4)

代码:

#include <bits/stdc++.h>
using  namespace  std;

#define ll __int64
#define rep(i,k,n) for(int i=k;i<=n;i++)

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}

ll dp[50][50];

int  main(){
  int k,n;
  read(n),read(k);
  rep(i, 0, n)dp[i][0] = 1;
  rep(i, 1, n){
    rep(j, 0, k){
      dp[i][j] = dp[i-1][j];
      rep(p, 1, i-1){
        rep(q, 0, j-1){
          dp[i][j] += dp[p-1][q] * dp[i-1-p][j-1-q];
        }
      }
    }
  }
  print(dp[n][k]);
  return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值