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.
codeforces 478D Red-Green Towers(dp)_第1张图片

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;
}



你可能感兴趣的:(dp,ACM,codeforces)