Codeforces 478D red-green Towers (DP)

Source: Internet
Author: User

        • Main topic
        • Thinking of solving problems
        • Reference Code

There is 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 and 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 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 have H levels can be built out of the available blocks.

Red-green Towers is considered different if there exists some level, which consists of red blocks in the one tower and Consists of green blocks in the other tower.

You is to write a program, that would find the number of different red-green towers of height H modulo 109?+?7.

Input
The only line of input contains-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 towers for the first sample.

Main topic

Use R Block Red, G block green block, build an H layer (as high as possible) tower, layer I has the same color of the box.

Ask about the number of possible options.

Thinking of solving problems

Dynamic planning

DP (H, r) represents the number of schemes with the R Block red in the H layer
The number of green can be determined by h,r.

DP (h, r) = DP (h-1, R-H) + DP (H-1, R)

Obviously scrollable compression, DP only related to the previous one, R reverse loop avoids referencing the modified data.
Dp[r] + = dp[r-h];

Reference Code
#include <iostream>#include <cstring>#include <cstdio>using namespace STD;Const intMAXN =200010;Const intMOD =1e9+7;intDP[MAXN], R, G, H, ans;voidInit () {memset(DP,0,sizeof(DP)); dp[0] =1; Ans =0;}voidSolve () {intH for(h =1; h* (H +1) <=2* (R+G); h++) { for(intr = Min (h* (H +1)/2, R); R >= H;        r--) {Dp[r] = (Dp[r] + dp[r-h])% MOD; }} H = h-1; for(intr = Max (0, h* (H +1)/2-G); R <= min (h* (H +1)/2, R);    r++) {ans = (ans + dp[r])% MOD; }printf("%d\n", ans);}intMain () { while(~scanf("%d%d", &r, &g)) {init ();    Solve (); }return 0;}

Codeforces 478D red-green Towers (DP)

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.