【CF Gym 100637K】 Microcircuits 【DP】【环上n个点,问不相交的连k条线的方案数】
2016-10-14 10:43
155 查看
传送门:
描述:
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
output
input
output
题意:
给你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
[k]);
return 0;
}
描述:
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
[k]);
return 0;
}
相关文章推荐
- CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)
- CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)
- CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)
- CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)
- [dp]cf gym 101485B; bzoj4426 Better Productivity(NWERC 2015 B)
- CFgym:Memory Manager(dp & 单调队列优化)
- CF 543A 【dp】
- c++使用动态规划dp(自底向上)重构解决钢条切割输出最大收益和切割方案及运行实例结果
- CF 453B Little Pony and Harmony Chest 解题报告(状态压缩DP)
- luogu P2066 机器分配[背包dp+方案输出]
- 【CF 731E】Funny Game(DP)
- HDU 5743 Join The Future 给定部分区间和奇偶,和数字范围,求方案数 DP
- [BZOJ1079][SCOI2008][DP]着色方案
- cf 127 div .1 Fragile Bridges(dp 好题)
- Gym 100812I Dragon Delivers(区间dp)
- [CF 148D]Bag of mice[概率DP]
- CF 219D Choosing Capital for Treeland (树形dp)
- CF 351A - Jeff and Rounding DP
- CF 455A(Boredom-dp)
- Gym - 100676G Training Camp (状压dp)