Codeforces Round #273 (Div. 2) D. Red-Green Towers 背包dp
2 seconds
256 megabytes
standard input
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.
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 the only integer — the number of different possible red-green towers of height h modulo 109 + 7.
4 6
2
9 7
6
1 1
2
The image in the problem statement shows all possible red-green towers for the first sample.
题意:有r个红方块,g个绿方块,第i层有i块,每层的方格颜色相同,层数为最多装的层数,求方案数;
思路:得到用i块红方块装这个red-green towers的方案,剩余用绿色装,判断是否够,够就加;
01背包方案数,复杂度(h*min(r,g));
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int h[];
int hh(int x)
{
int pos=upper_bound(h+,h+,x)-h;
return pos-;
}
int v[];
ll dp[N<<];
int main()
{
for(int i=;i<=;i++)
v[i]=i,h[i]=i*(i+)/;
int r,g;
scanf("%d%d",&r,&g);
int hig1=hh(r+g);
memset(dp,,sizeof(dp));
dp[]=;
for(int t=;t<=hig1;t++)
{
for(int i=r;i>=t;i--)
dp[i]+=dp[i-v[t]],dp[i]%=mod;
}
ll ans=;
for(int i=max(,h[hig1]-g);i<=r;i++)
ans+=dp[i],ans%=mod;
printf("%lld\n",ans);
return ;
}
Codeforces Round #273 (Div. 2) D. Red-Green Towers 背包dp的更多相关文章
- 贪心 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 ...
- Codeforces Round #233 (Div. 2) B. Red and Blue Balls
#include <iostream> #include <string> using namespace std; int main(){ int n; cin >&g ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #273 (Div. 2)D. Red-Green Towers DP
D. Red-Green Towers There are r red and g green blocks for construction of the red-green tower. Re ...
- 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 ...
随机推荐
- 《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用
原创文章,欢迎转载.转载请注明:关东升的博客 参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以 ...
- influxDB概念
一.基本概念 1)database--数据库,这个同传统数据库的数据库概念. 2)measurement--数据表,在InfluxDB中,measurement即为表的作用,同传统数据库中的table ...
- testlink1.9.3测试管理工具安装
一.下载testlink1.9.3rar安装包 下载地址:http://download.csdn.net/download/u010082992/7678491 二.安装IIS 在Server 20 ...
- setdefault函数的用法及个人理解
setdefault函数的用法及理解 dict.setdefault(key, default=None) 功能:如果键不存在于字典中,将会添加该键并将default的值设为该键的默认值,如果键存在于 ...
- [Erlang危机](5.1.3)进程
原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 联系邮箱:cto@188.com Processes Trying to ge ...
- centos7 安装vue
1: npm安装: 2: 报错: bash: vue: command not found 执行npm install --global vue-cli 后 执行 vue 报错 bash: vue: ...
- Junit单元测试注入spring中的bean(转载)
转载自:http://blog.csdn.net/cy104204/article/details/51076678 一般对于有bean注入的类进行方法单元测试时,会发现bean对象并没有注入进来,对 ...
- 常用MS-SQL写法整理
这里整理日常会用到的一些写法,一些常规的group by,系统函数等用法不在这里做记录了,大家有什么好的写法也可以分享下 1 sql操作xml内容(sp_xml_preparedocument和ope ...
- Dancing Link专题
一些链接: http://www.cnblogs.com/-sunshine/p/3358922.html http://www.cnblogs.com/grenet/p/3145800.html 1 ...
- Python基础(16)_面向对象程序设计(类、继承、派生、组合、接口)
一.面向过程程序设计与面向对象程序设计 面向过程的程序设计:核心是过程,过程就解决问题的步骤,基于该思想设计程序就像是在设计一条流水线,是一种机械式的思维方式 优点:复杂的问题的简单化,流程化 缺点: ...