Codeforces Round 273 (Div 2) D DP-roll array red-green towers two color blocks for top-jagged floors The total number of colors of each layer is the same as that of the item bank-CF

Source: Internet
Author: User
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. Sample Test (s) 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"

#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < ctype.h> #include <math.h> #include <set> #include <map> #include <vector> #include <queue
> #include <bitset> #include <algorithm> #include <time.h> using namespace std; void Fre () {freopen ("c://test//input.in", "R", stdin); Freopen ("C://test//output.out", "w", stdout); #define MS (X,y) memset (x,y,sizeof (x)) #define MC (x,y) memcpy (x,y,sizeof (x)) #define MP (x,y) Make_pair (x,y) #define LS o<<1 #define
RS o<<1|1 typedef long LL;
typedef unsigned long UL;
typedef unsigned int UI; Template <class t1,class t2>inline void Gmax (T1 &a,t2 b) {if (b>a) a=b;} template <class T1,class T2>inli
ne void Gmin (T1 &a,t2 b) {if (b<a) a=b;} const int n=900,m=2e5+2,z=1e9+7,ms63=1061109567;
int n,m;	int f[2][m];
	F[][i] Indicates that there is still a scheme number of I red squares void add (int &x,int y) {x+=y;
if (x>=z) x-=z; int main () {while ~scanf ("%d%d")&n,&m)) {//(top+1) top<=2 (n+m)//top+0.<=sqrt (2 (n+m)) int top=sqrt (n+m<<1);
		while ((1+top) *top/2> (n+m))--top;
		int sum=0;
		int now=0;
		int nxt=1;
		MS (f[now],0); f[0][n]=1;
			for (int i=1;i<=top;++i) {MS (f[nxt],0); for (int red=0;red<=n;++red)//The number of red before the enumeration {int green=m+n-sum-red;//can actually make red<+m+n-sum in the For loop, or add a lot of pruning restrictions
				To speed the if (green<0) break;
				if (red>=i) Add (f[nxt][red-i],f[now][red]);
			if (green>=i) Add (f[nxt][red],f[now][red]);
			} sum+=i;
			Now^=1;
		Nxt^=1;
		int ans=0;
		for (int red=0;red<=n;++red) Add (ans,f[now][red]);
	printf ("%d\n", ans);
return 0;

* * * "trick&&" to observe the real range of data, do not be very large numbers to scare Oh. "The" is a red-green two-color box, the number of R and G respectively.

There are 0<=r,g<=2e5,r+g>=1 we want to use this red and green squares (not all, can be part), forming a height as high as possible blocks.
This building block needs to satisfy the--1, assuming that the height is H, then the first layer has 1, the second layer has 2, ..., h layer H.

2, each layer of the color of the box, not all red, is all green.

Let you output, how many schemes can make up the tallest building blocks.
"Type" DP-scrolling array "Analysis" must be a good analysis of the size of the data. Although the number of R and G can be up to 2e5, however, in fact, the highest level can be reached, but only the highest floor height of the top, then we have-(top+1) TOP/2 <= n+m Obviously, (top+1) top<=2 (n+m), or top+0.
<= 2 (n+m) Obviously, we find that sqrt (2 (n+m)) is necessarily >=top.
Thus, a maximum of [sqrt (2 (n+m))] is obtained.

However, this top may exceed the actual largest floor, so we have--while ((1+top) *top/2> (n+m)--top;
Why, now the top must be the largest floor. Because the building of this floor, the total number is for 1~top and, so, this division, this number of split, can be extremely flexible way to match, so can (this is a man's intuition, hum.)

And the number of slices, we can cut out the largest layer.
So we have the maximum number of layers and it must be no more than 899.
So, the next direct use of f[i][j] means that the 1~i layer is now on, and the number of blocks of J Block Red is left.
Obviously have--1, initial condition is f[0][n]=1;
	The 2,DP equation is an enumeration of floors, enumerating the number of red blocks left over before all the floors have been paved, and then--we can find the number of green blocks that are left now, by the number of pieces of wood used altogether.
If there are more blocks of red wood or green wood than the amount of wood needed to shop the floor, we can update it-because space consumption can reach 900*2e5 and explode. So we can use the scrolling array to get AC. "Time Complexity && Optimization" O (sqrt (n+m) n) "data" input 200000 200000 output 206874596 */



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.