Description

有红色、绿色两种砖块,分别有 \(r,g\) 块,要用他们搭建高度最大的金字塔(第 \(i\) 层恰好有 \(i\) 块砖),每一层的砖的颜色必须相同,求有多少种方案。

Solution

考虑到层数是 \(\sqrt n\) 量级的,因此我们可以简单地 DP,不妨设计算出的层数为 \(h\),那么这样需要的总砖块数是确定的,不妨记为 \(tot\)

\(f[i][j]\) 表示制作了 \(1-i\) 层,用了 \(j\) 块红砖,有多少种方案。

#include <bits/stdc++.h>
using namespace std;

#define int long long 
const int N = 200005;
const int mod = 1e+9+7;

int f[N],n,r,g;

signed main()
{
    ios::sync_with_stdio(false);

    cin>>r>>g;

    for(int i=1;i<=r+g+1;i++)
    {
        int t=i*(i+1)/2;
        if(t>r+g) 
        {
            n=i-1;
            break;
        }
    }

    f[0]=1;

    for(int i=1;i<=n;i++)
    {
        for(int j=r;j>=i;j--)
        {
            f[j]=(f[j]+f[j-i])%mod;
        }
    }

    int max_r=r;
    int min_r=n*(n+1)/2-g;

    int ans=0;
    for(int i=max(0ll,min_r);i<=max_r;i++)
    {
        ans=(ans+f[i])%mod;
    }

    cout<<ans<<endl;
}

相关文章: