cf478D Red-Green Towers

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 - 1blocks, 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·105, r + g ≥ 1).

Output

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

Examples
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.

最大的h就等于r+g最多能叠几层,等差数列,求出h之后,就是算从1-h中挑选若干个数,这些数相加等于r,方案数就是答案了。前者直接枚举,后者是一个dp,但是题目r+g并没有说恰好能组成h层,假如多出x个颜色块,我们需要取d=max(r,g),dp[d]+dp[d-1]+……dp[d-x]就是答案了,因为我们至少要用到d-x个某种颜色块,多加x个也行,那另一种颜色块就可以少用x个,全部加起来就是所有方案

这个比较坑= =还好样例就有这种情况,不然WA都不知道WA在哪.

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
typedef long long ll;
#define X first
#define Y second
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define sd(x) scanf("%d",&(x))
#define Pi acos(-1.0)
#define sf(x) scanf("%lf",&(x))
#define ss(x) scanf("%s",(x))
#define maxn 10000000
#include <ctime>
const int inf=0x3f3f3f3f;
const long long mod=1000000007;
using namespace std;
int dp[400086];
int main()
{
    #ifdef local
    freopen("in","r",stdin);
    //freopen("data.txt","w",stdout);
    int _time=clock();
    #endif
    int h=0;
    int r,g;
    cin>>r>>g;
    int f=r+g;
    while(f>0)
    {
        if(h+1>f)break;
        h++;
        f-=h;
    }
    int k=0;
    dp[0]=1;
    for(int i=1;i<=h;i++)
    {
        for(int j=k;j>=0;j--)
        {
            dp[i+j]=(dp[j]+dp[i+j])%mod;
            k=max(k,i+j);
        }
    }
    int ans=0;
    r=max(r,g);
    for(int i=r;i>=r-f;i--)
        ans=(ans+dp[i])%mod;
    cout<<ans<<endl;
    #ifdef local
    printf("time: %d\n",int(clock()-_time));
    #endif
}
View Code

 

转载于:https://www.cnblogs.com/scau-zk/p/5654499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值