zoukankan      html  css  js  c++  java
  • cf478D Red-Green Towers

    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 - 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.

    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.

    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.

    最大的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在哪.

    #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
    ",int(clock()-_time));
        #endif
    }
    View Code
  • 相关阅读:
    python实现决策树
    ag 命令的帮助文档
    Linux rsync 命令学习
    常用数学符号读法及其含义
    Python 数据分析
    Django 创建项目笔记
    Python 实用技巧
    Python 必备好库
    Pytest 简明教程
    Python 打包中 setpy.py settuptools pbr 的了解
  • 原文地址:https://www.cnblogs.com/scau-zk/p/5654499.html
Copyright © 2011-2022 走看看