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 ...
随机推荐
- 巨蟒django之CRM4 一些小功能
内容回顾: 修改的地方 (1) (2) (3) (4) (5) 整体回顾前几天内容: 现在可以登录的原因,session内部存储了信息 这个时候我们再访问刚才的地址,会发现,跳转到了登录页面login ...
- Object-Oriented Metrics: LCOM 内聚性的度量
Object-Oriented Metrics: LCOM https://www.computing.dcu.ie/~renaat/ca421/LCOM.html Object-Oriented M ...
- Android的代码都得自己一个个敲一遍吗?
近期在Android学习中,碰到一个头疼的问题.众所周知Android是一个开发源码的平台,网上有非常多网友分享的关于各种样例的demo,比方扫描二维码,瀑布流等,对于前人已有的成果,我们 ...
- 004-JVM指令集(指令码、助记符、功能描述)
一.JVM指令助记符 1)操作数栈 变量到操作数栈:iload,iload_,lload,lload_,fload,fload_,dload,dload_,aload,aload_ 操作数栈到变量:i ...
- rails generator
generate 查找顺序 rails/generators/initializer/initializer_generator.rb generators/initializer/initializ ...
- Python 开发者节省时间的 10 个小技巧
Python 是一个美丽的语言,可以激发用户对它的爱.所以如果你试图加入程序员行列,或者你有点厌倦C++,Perl,Java 和其他语言,我推荐你尝试Python. Python有很多吸引程序员的功能 ...
- Hibernate深入浅出(九)持久层操作——数据保存&批量操作
数据保存: 1)session.save session.save方法用于实体对象到数据库的持久化操作.也就是说,session.save方法调用与实体对象所匹配的Insert SQL,将数据插入 ...
- FTP下载
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import ja ...
- pdo封装2
<?php //添加了一个 _createSql 方法,负责创建所有sql class Db{ static private $ins; private $pdo; private $table ...
- xml转换为对象 微信接口
public sealed class XMLSerilizable { /// <summary> /// XML转换为对象 /// </summary> /// <t ...