题意:
给出r个红色方块和g个绿色方块。现在问这些方可可以搭成多少种塔。塔的高度要尽量最高。第一层一个方块第二层两个...最后以层h个。高度为h。每层只能是红色方块或者绿色方块。
题解:
之前想的状态时这个dp[i][j][k]到第i层位置用了j个红块,和k个绿块。内存明显不够。
那么优化下dp[i][j]表示到第i层位置红块的用掉的个数,那么绿块很容易可以得出。但是内存还是不够用,急促优化,滚动数组。然后就可以ac了。
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
typedef long long lld;
const int oo=0x3f3f3f3f;
const lld OO=1e18;
const int Mod=1000000007;
const int maxn=1000;
const int maxm=200005;
lld dp[2][maxm];
int main()
{
int r,g,h;
scanf("%d %d",&r,&g);
h=(sqrt(1.0+8.0*(r+g))-1.0)/2.0;
memset(dp,0,sizeof dp);
dp[0][0]=1;
for(int i=0;i<h;i++)
{
int pre=i*(i+1)/2;///pre-j表示绿色用掉的个数
memset(dp[(i+1)%2],0,sizeof dp[(i+1)%2]);
for(int j=0;j<=r&&j<=pre;j++)
{
if(j+i+1<=r)///红色用掉的个数不能多于r
dp[(i+1)%2][j+i+1]=(dp[(i+1)%2][j+i+1]+dp[i%2][j]+Mod)%Mod;
if(pre-j+i+1<=g)///绿色用掉的个数不能多于g
dp[(i+1)%2][j]=(dp[(i+1)%2][j]+dp[i%2][j]+Mod)%Mod;
}
}
lld ans=0;
for(int i=0;i<=r;i++)
ans=(ans+dp[h%2][i]+Mod)%Mod;
printf("%I64d\n",ans);
return 0;
}