D. Red-Green Towers
 

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·105, r + 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
 
Note

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

题意:给你 r,g,分别表示红色,绿色方块的数目,现在如题图所示,叠方块:满足每一行都是同一种颜色

问你方案数是多少。

题解:  一眼dp

我们可以先想到:dp[i][j]表示 从底层叠到i层  红色方块用了j个的方案数 显然绿色用了(i)*(i+1)/2-j;

我们就能想到转移方程了很简单。

然后,你会发现这就是个背包,dp[894][200000]会爆内存,我们可以用滚动数组来 省掉一维,dp[j]表示修建了n层红色方块用j的方案数,我们必须处理处最少的j的第一种方案...........

///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define inf 100000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//****************************************
#define maxn 200000+5
int mod=;
int n;
ll dp[maxn],f[maxn];
int main(){ int a=read(),b=read();
for(int i=;;i--){
if((i*(i+)/)<=(a+b)){
n=i;break;
}
}int next=;
for(int i=;;i--){
if((i*(i+)/)<=(b)){
next=i;break;
}
}//cout<<n<<endl;
dp[]=;
// cout<<next<<endl;
for(int i=;i<=n;i++){
for(int j=;j<=a;j++){f[j]=dp[j];dp[j]=;if(j>(i*(i+)/))break;}
for(int j=;j<=a;j++){
if(((i-)*(i)/-j+i)<=b)
dp[j]=(dp[j]+f[j])%mod;
if(j-i>=)
dp[j]=(dp[j]+f[j-i])%mod;
if(j>(i*(i+)/))break;
}
}
int ans=;
for(int i=;i<=a;i++){
// cout<<dp[i]<<" "<<f[i]<<endl;
ans=(ans+dp[i])%mod;
}
printf("%d\n",ans);
return ;
}

代码

Codeforces Round #273 (Div. 2)D. Red-Green Towers DP的更多相关文章

  1. 贪心 Codeforces Round #273 (Div. 2) C. Table Decorations

    题目传送门 /* 贪心:排序后,当a[3] > 2 * (a[1] + a[2]), 可以最多的2个,其他的都是1个,ggr,ggb, ggr... ans = a[1] + a[2]; 或先2 ...

  2. Codeforces Round #233 (Div. 2) B. Red and Blue Balls

    #include <iostream> #include <string> using namespace std; int main(){ int n; cin >&g ...

  3. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  4. Codeforces Round #273 (Div. 2)-C. Table Decorations

    http://codeforces.com/contest/478/problem/C C. Table Decorations time limit per test 1 second memory ...

  5. Codeforces Round #273 (Div. 2) A , B , C 水,数学,贪心

    A. Initial Bet time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. codeforces 的 Codeforces Round #273 (Div. 2) --C Table Decorations

    C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Codeforces Round #273 (Div. 2)C. Table Decorations 数学

    C. Table Decorations   You have r red, g green and b blue balloons. To decorate a single table for t ...

  8. Codeforces Round #273 (Div. 2) D. Red-Green Towers 背包dp

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

  9. Codeforces Round #273 (Div. 2)-B. Random Teams

    http://codeforces.com/contest/478/problem/B B. Random Teams time limit per test 1 second memory limi ...

随机推荐

  1. spring中配置数据源

    spring中配置数据源的几种常见方式: #mysql 数据库配置(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.u ...

  2. 【DVWA】【SQL Injection】SQL注入 Low Medium High Impossible

    1.初级篇 low.php 先看源码,取得的参数直接放到sql语句中执行 if( isset( $_REQUEST[ 'Submit' ] ) ) { // Get input $id = $_REQ ...

  3. 第二节:1_C#中的委托的使用和讲解(转)

    C# 中的委托 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容 ...

  4. uploadify的简单使用

    简单的图片上传: 1.进入官网下载uploadify插件:http://www.uploadify.com/download/ 2.导入uploadify插件提供的css样式和类库: <link ...

  5. 自定义属性Attribute的运用

    有时候需要一个枚举类,能够承载更多的信息,于是可以利用attribute这个特性. 首先编写自己业务需求类 [AttributeUsage(AttributeTargets.Field)] publi ...

  6. 如何防止XshellPortable、putty、SecureCRT等断网造成Linux命令中断

    在使用XshellPortable.putty.SecureCRT等工具远程连接Linux系统时,如果我们执行了一大堆命令,在命令尚未执行完毕,客户端突然断网或者XshellPortable.putt ...

  7. Go:值类型、引用类型

    值类型,变量存的就是值本身: in系列t.float系列.bool.string.数组和struct 引用类型,变量存的是一个地址,这是地址存的才是值本身: 指针.slice.map.chan.int ...

  8. APUE 文件IO

    文件 IO 记录书中的重要知识和思考实践部分 Unix 每个文件都对应一个文件描述符(file descriptor),为一个非负整数,一个文件可以有多个fd, 后面所有与文件(设备,套接字等)有关操 ...

  9. java 十六周总结

  10. gnuplot examples

    xy plot #set terminal jpeg #set output 'alfa.jpg' set terminal postscript eps font 24 set out 'U_vs_ ...