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 ...
随机推荐
- C#快速整理代码格式
删除最后一个大括号,再添加.vs自动整理代码结构.
- mysql_表_操作
1.创建表 # 基本语法: create table 表名( 列名 类型 是否可以为空 默认值 自增 主键, 列名 类型 是否可以为空 )ENGINE=InnoDB DEFAULT CHARSET=u ...
- 信息搜集之常见的web组合
环境: Win2003或Win7 Asp解析环境搭建 小旋风 Php解析环境搭建 phpstudy aspx解析环境搭建 IIS jsp解析环境搭建 jspstudy 常规渗透环境网络环境解析 1.操 ...
- 【转】@javax.ws.rs Webservice注解
用于webservice. 1.路径 @javax.ws.rs.Path 标识要请求的资源类或资源方法的uri路径. 例,@Path("animal"),表示下一层路径是anima ...
- JVM 指令讲解
挺有意思的 转载记录下 转载自 https://www.cnblogs.com/f1194361820/p/8524666.html 原作者: 房继诺 JVM 指令 1.Demo 2.Clas ...
- centos添加定时任务
安装crontab: yum install crontabs 查看crontab服务状态:service crond status 手动启动crontab服务:service crond start ...
- 单例 与 static
单例的构造器是private的,不能直接用new 创建对象.static虽然可以随时使用,但是还是有被重新创建的可能. 举个例子,你希望任何时候有一个class A的实例就可以了class B { ...
- C++ Primer笔记14_面向对象程序设计
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/scottly1/article/details/31371611 OOP概述 面向对象程序设计(ob ...
- C# 截屏函数
截屏函数: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; ...
- C#中的foreach和yield
1. foreach C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. foreach (Person p in persons) { Console.WriteLine ...