Codeforces Round #273 (Div. 2)D. Red-Green Towers DP
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
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的更多相关文章
- 贪心 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 #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- 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 time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
随机推荐
- 防止按钮button重复提交,点击后失效,10秒后恢复
<script type="text/javascript"> $(function () {//页面完全加载完后执行 /*防止重复提交 10秒后恢复*/ var is ...
- 【C++】朝花夕拾——中缀转后缀
对于简单的四则运算而言,后缀表达式可以通过使用栈(stack)快速算出结果 ==================================我是分割线======================= ...
- Clistctrl使用
CListCtrl控件使用方法总结 今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下: 以下未经说明,listctrl默认view 风格为report 相关类 ...
- 07C语言程序语句
C语言程序语句 判断语句 if(表达式) {语句} #include <stdio.h> int main(){ printf("请输入2个数字:"); int a,b ...
- Python基础之字符的编码
参考原文 Python廖雪峰 为什么要进行编码? 计算机只能处理二进制数字(0100111),要处理文本,就必须先把文本转为数字才能处理,这个过程就叫编码. 字符的编码 ASCII编码 由于计算机是美 ...
- 09.C语言:预处理(宏定义)、字节序、地址对齐
一.预处理 预处理 gcc -E Hello.c -o hello.i 编译 gcc -S hello.i -o hello.s 汇编 gcc -c hello.s -o hello.o 链接 gcc ...
- ACM数论常用知识完全解读
此版本纯属扯淡....... 一个一个来起.
- 【页面传值6种方式】- 【JSP 页面传值方法总结:4种】 - 【跨页面传值的几种简单方式3种】
阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 Session JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总 ...
- XOR的艺术
题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[l,r ...
- ajax多文件上传,js原生ajax请求(转)
function uploadImageFile(){ var xhr = new XMLHttpRequest(); //定义表单变量 var file = document.getElementB ...