Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) D. Singer House(dp)
2017-07-22 10:56
501 查看
题目链接:http://codeforces.com/contest/830/problem/D
dp[i][j]表示i层,有j个不相容的路径的方案数,每次加入一个根节点有四种情况,选根节点为一个单独的路径,不选根节点,选根节点和左/右子树某个路径相连,选根节点将两个路径连在一起。用long long会T,具体转移方程看题解。
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=505;
const int MOD=1e9+7;
typedef long long ll;
int dp[MAXN][MAXN];
void add(int &a,int b)
{
a+=b;
if(a>=MOD)
a-=MOD;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
dp[1][0]=1,dp[1][1]=1;
for(int i=1;i<n;i++)
{
for(int l=0;l<=n;l++)
{
for(int r=0;r<=n;r++)
{
int sum=((ll)dp[i][l])*dp[i][r]%MOD;
if(l+r+1<=n)
add(dp[i+1][l+r+1],sum);
if(l+r<=n)
{
add(dp[i+1][l+r],sum);
add(dp[i+1][l+r],(ll)sum*2%MOD*(l+r)%MOD);
}
if(l+r-1<=n&&l+r-1>=0)
{
add(dp[i+1][l+r-1],(ll)sum*((l+r)*(l+r-1)/2)%MOD*2%MOD);
}
}
}
}
printf("%d\n",dp
[1]);
return 0;
}
dp[i][j]表示i层,有j个不相容的路径的方案数,每次加入一个根节点有四种情况,选根节点为一个单独的路径,不选根节点,选根节点和左/右子树某个路径相连,选根节点将两个路径连在一起。用long long会T,具体转移方程看题解。
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=505;
const int MOD=1e9+7;
typedef long long ll;
int dp[MAXN][MAXN];
void add(int &a,int b)
{
a+=b;
if(a>=MOD)
a-=MOD;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
dp[1][0]=1,dp[1][1]=1;
for(int i=1;i<n;i++)
{
for(int l=0;l<=n;l++)
{
for(int r=0;r<=n;r++)
{
int sum=((ll)dp[i][l])*dp[i][r]%MOD;
if(l+r+1<=n)
add(dp[i+1][l+r+1],sum);
if(l+r<=n)
{
add(dp[i+1][l+r],sum);
add(dp[i+1][l+r],(ll)sum*2%MOD*(l+r)%MOD);
}
if(l+r-1<=n&&l+r-1>=0)
{
add(dp[i+1][l+r-1],(ll)sum*((l+r)*(l+r-1)/2)%MOD*2%MOD);
}
}
}
}
printf("%d\n",dp
[1]);
return 0;
}
相关文章推荐
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)-模拟-C. Jury Marks-
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) ..C. Jury Marks
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) D. Best Edge Weight(最小生成树+LCA+树链剖分)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) 总结
- 【贪心】Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A. String Reconstruction
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
- Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) C. Bamboo Partition(数学)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
- Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) C. Bear and Company(DP)
- 【构造】Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) B. High Load