D. Red-Green Towerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere 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 - 1blocks, 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.
InputThe 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).
OutputOutput the only integer — the number of different possible red-green towers of height h modulo 109 + 7.
Examplesinput4 6output2input9 7output6input1 1output2NoteThe image in the problem statement shows all possible red-green towers for the first sample.
最大的h就等于r+g最多能叠几层,等差数列,求出h之后,就是算从1-h中挑选若干个数,这些数相加等于r,方案数就是答案了。前者直接枚举,后者是一个dp,但是题目r+g并没有说恰好能组成h层,假如多出x个颜色块,我们需要取d=max(r,g),dp[d]+dp[d-1]+……dp[d-x]就是答案了,因为我们至少要用到d-x个某种颜色块,多加x个也行,那另一种颜色块就可以少用x个,全部加起来就是所有方案
这个比较坑= =还好样例就有这种情况,不然WA都不知道WA在哪.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack> typedef long long ll; #define X first #define Y second #define mp(a,b) make_pair(a,b) #define pb push_back #define sd(x) scanf("%d",&(x)) #define Pi acos(-1.0) #define sf(x) scanf("%lf",&(x)) #define ss(x) scanf("%s",(x)) #define maxn 10000000 #include <ctime> const int inf=0x3f3f3f3f; const long long mod=1000000007; using namespace std; int dp[400086]; int main() { #ifdef local freopen("in","r",stdin); //freopen("data.txt","w",stdout); int _time=clock(); #endif int h=0; int r,g; cin>>r>>g; int f=r+g; while(f>0) { if(h+1>f)break; h++; f-=h; } int k=0; dp[0]=1; for(int i=1;i<=h;i++) { for(int j=k;j>=0;j--) { dp[i+j]=(dp[j]+dp[i+j])%mod; k=max(k,i+j); } } int ans=0; r=max(r,g); for(int i=r;i>=r-f;i--) ans=(ans+dp[i])%mod; cout<<ans<<endl; #ifdef local printf("time: %d\n",int(clock()-_time)); #endif }