codeforces 478D Red-Green Towers(dp)

题目链接

D. Red-Green Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are r red and g green blocks for construction of the red-green tower. Red-green tower can be built following next rules:

  • Red-green tower is consisting of some number of levels;

  • Let the red-green tower consist of n levels, then the first level of this tower should consist of n blocks, second level — of n - 1 blocks, the third one — of n - 2 blocks, and so on — the last level of such tower should consist of the one block. In other words, each successive level should contain one block less than the previous one;

  • Each level of the red-green tower should contain blocks of the same color.

Let h be the maximum possible number of levels of red-green tower, that can be built out of r red and g green blocks meeting the rules above. The task is to determine how many different red-green towers having h levels can be built out of the available blocks.

Two red-green towers are considered different if there exists some level, that consists of red blocks in the one tower and consists of green blocks in the other tower.

You are to write a program that will find the number of different red-green towers of height h modulo 109 + 7.

Input

The only line of input contains two integers r and g, separated by a single space — the number of available red and green blocks respectively (0 ≤ r, g ≤ 2·105r + g ≥ 1).

Output

Output the only integer — the number of different possible red-green towers of height h modulo 109 + 7.

Sample test(s)
input
4 6
output
2
input
9 7
output
6
input
1 1
output
2
Note

The image in the problem statement shows all possible red-green towers for the first sample.

题意:有r个红色和g个绿色的方块,用它们堆塔。塔必须满足两个条件。第一,最顶层为1个方块,次顶层为2个方块,以此类推。第二,每层的方块的颜色必须相同。问塔的高度最高的情况下,有多少种堆塔的方式。(若两座塔有一层的颜色不同则堆塔方式不同)。

题解:层数最多大概为sqrt(r+g) 层。所以 我们可以用dp[i][j] ,表示从顶层开始建,当前建好了i 层,已经用了j 个红色的方块的方案数。我们已知层数和使用的红色方块数,可以得出使用的绿色方块数。转移很简单。由于空间开销太大,所以要用滚动数组优化。

代码如下:

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#define inff 0x3fffffff
#define eps 1e-8
#define nn 210000
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int dp[2][nn];
bool use[2][nn];
int r,g;
int main()
{
    int i,j;
    while(scanf("%d%d",&r,&g)!=EOF)
    {
        int tem;
        for(i=0;i<=r;i++)
        {
            use[0][i]=false;
            dp[0][i]=0;
        }
        dp[0][0]=1;
        use[0][0]=true;
        int t=1;
        int ans=0;
        for(i=1;;i++)
        {
            for(j=0;j<=r;j++)
            {
                dp[t][j]=0;
                use[t][j]=false;
                tem=(i-1)*i/2;
                if(g-tem+j>=i)
                {
                    if(use[1-t][j])
                    {
                        dp[t][j]=(dp[t][j]+dp[1-t][j])%mod;
                        use[t][j]=true;
                    }
                }
                if(j>=i)
                {
                    if(use[1-t][j-i])
                    {
                        use[t][j]=true;
                        dp[t][j]=(dp[t][j]+dp[1-t][j-i])%mod;
                    }
                }
            }
            for(j=0;j<=r;j++)
            {
                if(use[t][j])
                    break;
            }
            if(j<=r)
            {
                t=1-t;
                continue;
            }
            else
            {
                for(j=0;j<=r;j++)
                {
                    ans=(ans+dp[1-t][j])%mod;
                }
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值