• Codeforces Round #273 (Div. 2)D. Red-Green Towers DP


    D. Red-Green Towers
     

    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·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
     
    Note

    The image in the problem statement shows all possible red-green towers for the first sample.

    题意:给你 r,g,分别表示红色,绿色方块的数目,现在如题图所示,叠方块:满足每一行都是同一种颜色

           问你方案数是多少。

    题解:  一眼dp

             我们可以先想到:dp[i][j]表示 从底层叠到i层  红色方块用了j个的方案数 显然绿色用了(i)*(i+1)/2-j;

            我们就能想到转移方程了很简单。

             然后,你会发现这就是个背包,dp[894][200000]会爆内存,我们可以用滚动数组来 省掉一维,dp[j]表示修建了n层红色方块用j的方案数,我们必须处理处最少的j的第一种方案...........

    ///1085422276
    #include<bits/stdc++.h>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define inf 100000
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //****************************************
    #define maxn 200000+5
    int  mod=1000000007;
    int n;
    ll dp[maxn],f[maxn];
    int main(){
    
        int a=read(),b=read();
        for(int i=2000;;i--){
            if((i*(i+1)/2)<=(a+b)){
               n=i;break;
            }
        }int next=0;
         for(int i=2000;;i--){
            if((i*(i+1)/2)<=(b)){
               next=i;break;
            }
        }//cout<<n<<endl;
        dp[0]=1;
       // cout<<next<<endl;
         for(int i=1;i<=n;i++){
            for(int j=0;j<=a;j++){f[j]=dp[j];dp[j]=0;if(j>(i*(i+1)/2))break;}
            for(int j=0;j<=a;j++){
                if(((i-1)*(i)/2-j+i)<=b)
                dp[j]=(dp[j]+f[j])%mod;
                if(j-i>=0)
                dp[j]=(dp[j]+f[j-i])%mod;
                if(j>(i*(i+1)/2))break;
            }
         }
         int ans=0;
         for(int i=0;i<=a;i++){
               // cout<<dp[i]<<" "<<f[i]<<endl;
            ans=(ans+dp[i])%mod;
         }
    printf("%d
    ",ans);
       return 0;
    }
    代码
  • 相关阅读:
    树链剖分-bzoj1036
    POJ3489企鹅
    51nod 1130
    51nod-8-16
    51nod-8-15
    51nod 8-14
    51nod1582-n叉树
    51nod1574排列转换
    51nod1785数据流中的算法
    iOS开发--Swift 最近项目开发中遇到的一些小问题与解决方法
  • 原文地址:https://www.cnblogs.com/zxhl/p/4926069.html
Copyright © 2020-2023  润新知