【打CF,学算法——五星级】CodeForces 478D (dp计数)

本文详细介绍了如何利用动态规划解决CodeForces 478D问题,即用红绿两种方块构建特定规则的塔并计算不同构造方法的数量。通过分析问题,确定最高层数,并使用滚动数组优化空间复杂度,最终求得符合条件的塔的构建方法数量模10^9 + 7的结果。
摘要由CSDN通过智能技术生成

【CF简介】

提交链接:CF 478D


题面:

D. Red-Green Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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.

Examples
Input
4 6
Output
2
Input
9 7
Output
6
Input
1 1
Output
2
Note

The image in the problem statement shows all possible red-green towers for the first sample.

题意:

     给定红绿两种方块,要求以1,2,3..n的方式,一层一层垒上去,且任意一层只能是单色,问在该种方式下垒到最高高度的方式有多少种?


解题:

     因为是1,2,3..所以只要刚刚好方块够到某一层的总数量的话,是一定可以构造出可行方案的,因此可以直接根据两种方块数量总和,先求出最高高度。根据范围,最高高度为1000。可以比较轻松地想到用dp[i][j]来表示,其中i为第几层,j为到该层为止用了多少红色方块,dp的值的含义是在第i层用了j块红色方块的方案数。此方法看似可行,实则不然,1000*(2*10^5)远超出题目给的空间限定范围,且复杂度也够呛。因为,我们最后要取的仅仅是最后一层的结果,前面的计算过程都是不需要的,因此,我们可以采用滚动数组的方法,将第一维压缩至2,分别表示上一层和下一层。在计算过程中用队列或其他数据结构保存要更新的点。最后取最后一层的所有结果和即可。


代码:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#include <vector>
#define LL long long
#define maxn 200010
#define sq(a)  1LL*(a)*(a)
#define mod 1000000007
using namespace std;
bool vis[maxn];
vector <int> v[2];
int amount[1000];
int dp[2][maxn];
void init()
{
	for(int i=1;i<1000;i++)
		amount[i]=(1+i)*i/2;
}
int main()
{
	int r,g,total,h,a,b,sz,tmp,ans=0,tmp2;
	init();
	scanf("%d%d",&r,&g);
    total=r+g;
    h=(-1+sqrt(1+8.0*total))/2;
	if(g>0)
	{
		dp[0][0]=1;
		v[0].push_back(0);
	}
	if(r>0)
	{
		dp[0][1]=1;
		v[0].push_back(1);
	}

	a=0;
	b=1;
	for(int i=2;i<=h;i++)
	{
	   sz=v[a].size();
	   for(int i=0;i<sz;i++)
		   vis[v[a][i]]=0;
	   for(int j=0;j<sz;j++)
	   {

		   tmp=amount[i]-v[a][j];
		   if(tmp<=g)
		   {
			   if(!vis[v[a][j]])
			   {
				   v[b].push_back(v[a][j]);
				   dp[b][v[a][j]]=0;
				   vis[v[a][j]]=1;
			   }
			   dp[b][v[a][j]]=(dp[b][v[a][j]]+dp[a][v[a][j]])%mod;
		   }
		   tmp=v[a][j]+amount[i]-amount[i-1];
           if(tmp<=r)
		   {
			   if(!vis[tmp])
			   {
				   v[b].push_back(tmp);
				   vis[tmp]=1;
				   dp[b][tmp]=0;
			   }
			   dp[b][tmp]=(dp[b][tmp]+dp[a][v[a][j]])%mod;
		   }
		 }
	   v[a].clear();
	   swap(a,b);
	}
	sz=v[a].size();
	for(int i=0;i<sz;i++)
		ans=(ans+dp[(h+1)%2][v[a][i]])%mod;
	printf("%d\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值