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. Windows Server 2008不能Ping改为允许的方法

    用了Windows Server 2008朋友肯定都知道,2008在很多设置方面与2003不同,尤其在安全上进行了加强,例如:默认情况下Windows 2008是不允许PING的,那么如何打开允许PI ...

  2. vim之vimrc配置文件

    """"""""""""""""&quo ...

  3. dom4j使用方法详解

    本文先做知识点的简单介绍,最后附完整案例. 一.解析XML文件 public class Foo { //url为XML文档地址 //自己封装了一个工具类 返回解析完成的document public ...

  4. 如何快速的vue init 属于自己的vue模板?

    相信很多接触过vue的小伙伴非常熟悉了,我们在开启项目之前都需要vue init webpack xx来初始化自己的项目目录.但是在实际开发中我们往往会根据公司要求或者业务的需要会对目录进行局部的调整 ...

  5. 3D NAND闪存是个啥?让国内如此疯狂

    Repost: https://news.mydrivers.com/1/477/477251.htm 上个月底武汉新芯科技主导的国家级存储器产业基地正式动工,在大基金的支持下该项目将投资240亿美元 ...

  6. Python基础之简介

    参考原文 廖雪峰Python教程 什么是Python? Python是一种计算机程序设计语言,又被称为胶水语言,它是高级的编程语言. Python能干什么? 网站后端程序员.自动化运维.数据分析师.游 ...

  7. TWaver MONO Design中动画的导出与播放

    使用过MONO编辑器的朋友都应该了解MONO中支持一些动画的设置,比如开门,开窗,弹出设备,旋转场景,镜头巡航等.但如何将这些动画应用到自己的场景中呢?比如我们在编辑器中给机柜定义了动画,怎样让做好的 ...

  8. TFRecordReader "OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 1, current size 0)" 问题原因总结;

    1. tf.decode_raw(features['image_raw'],tf.uint8) 解码时,数据类型有没有错?tf.float32 和tf.uint8有没有弄混??? 2. tf.tra ...

  9. Java基本数据类型、包装类与String类之间的转换

    一.基本数据类型与包装类之间的转换: import org.junit.Test; public class MainTest { /** * 基本数据类型与包装类之间的转换 */ @Test pub ...

  10. 39页第3题 求x的n次幂

    /*计算x的n次幂*/ #include<stdio.h> main(void) { int i,n; double x,y; printf("Enter x:");/ ...