Codeforces 478d-red-green towers__codeforces

Source: Internet
Author: User
Topic

D. Red-green Towers time limit/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 "a" 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 bloc K. 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 is the maximum possible number of levels of Red-green tower, that can is built out of R red and G green blocks Meeti Ng the rules above. The task is to determine how many different Red-green towers has h levels can be built out of the available.

Two red-green towers are considered different if there exists level, this some of red consists in the one blocks and Consists of green blocks in the other tower.

You are are to write a program that'll 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 for the "the"

topic meaning

Give R a Red Square, g a green square, to put the shape on the map, that is, I want to have I, each layer is a color, and to the maximum height can be achieved, the number of species required to be able to pose. Solution

Set h as the maximum height, you can know that H max is 1000 less than, set Dp[i][j] said from the top down, to the first tier I, the red box has the number of J, then

dp[i+1][j]=dp[i+1][j]+dp[i][j]//The i+1 layer does not put red squares

dp[i+1][j-i-1]=dp[i+1][j-i-1]+dp[i][j]//the situation of the square Red Square of i+1 layer

Need to scroll array to optimize space code

#include <cstdio>
#include <cstring>
#define MOD 1000000007
int dp[2][200005],sum[1005];
int main ()
{
	int r,g,i,h,j;
	scanf ("%d%d", &r,&g);
	if (r==0| | g==0)
	{
		printf ("1\n");
		return 0;
	}
	sum[0]=0;
	for (I=1;; i++)
	{
		sum[i]=sum[i-1]+i;
		if (sum[i]>r+g) break
			;
	h=i-1;
	Dp[1][r]=1;
	Dp[1][r-1]=1;
	for (i=1;i


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.