Codeforces Round #273 (Div. 2)(D)dp,滚动数组

来源:互联网 发布:德旭交规软件 编辑:程序博客网 时间:2024/05/17 04:11

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·105r + 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.



题意:给你n个红块,m个绿块,要搭出一个塔,第K层有K个,且每一层颜色必须全部相同,问在塔搭出最高的情形下有多少种搭的方法。



题解:首先确定最大高度H,那么(H+1)*H/2<=m+n,由不等式求出最值H,这里DP[]I[J]表示在塔的第i层的时候,剩下J个红块的可能数,如果绿块也大于i,那么就将当前更新,这里需要使用滚动数组,如果直接开二维数组需要1e8的空间,使用2个变量next和now交替使用,使用异或运算实现滚动。在每次在使用前应该初始化一遍下一状态的数组。


#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#include<bitset>#include<utility>#include<functional>#include<iomanip>#include<sstream>#include<ctime>#include<cassert>using namespace std;#define N int(4e5+10)#define inf int(0x3f3f3f3f)#define mod int(1e9+7)typedef long long LL;#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)#define lld "%I64d"#else#define lld "%lld"#endif#ifdef CDZSC#define debug(...) fprintf(stderr, __VA_ARGS__)#else#define debug(...) #endifint dp[2][N];int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);int _time_jc = clock();#endifint n, m;while (~scanf("%d%d", &n, &m)){int h = sqrt((m + n) << 1);while (h*(h + 1) / 2 > m + n)h--;int now = 0, next = 1, sum = 0;memset(dp, 0, sizeof(dp));dp[0][n] = 1;for (int i = 1; i <= h; i++){memset(dp[next], 0, sizeof(dp[next]));for (int red = 0; red <= n; red++){int green = n + m - red - sum;if (red >= i)dp[next][red - i] = (dp[next][red - i] + dp[now][red]) % mod;if (green >= i)dp[next][red] = (dp[next][red] + dp[now][red]) % mod;}sum += i;next ^= 1;now ^= 1;}int ans = 0;for (int i = 0; i <= n; i++)ans =(ans+ dp[now][i])%mod;printf("%d\n", ans);}#ifdef CDZSCdebug("time: %d\n", int(clock() - _time_jc));#endifreturn 0;}
























0 0
原创粉丝点击