• UVa 10313 Pay the Price(类似数字分解DP)


    题意:

    把一个整数i拆分成不大于j的数字组合。

    思路:

    f[i, j]表示i可以拆分成多少个不大于j的数字组合。

    1. 包括j : f[i, j] += f[i-j, j-1];

    2. 不包括j : f[i, j] += f[i-j, j-1];

    可以收缩为一维数组,具体见代码:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <climits>
    
    long long int f[310];
    int n, a, b;
    char str[100];
    
    void solve()
    {
        memset(f, 0, sizeof(f));
        f[0] = 1;
    
        for (int i = 1; i <= b; ++i)
            for (int j = i; j <= n; ++j)
                f[j] += f[j-i];
        
        long long int ans = f[n];
    
        memset(f, 0, sizeof(f));
        f[0] = 1;
    
        for (int i = 1; i <= a - 1; ++i)
            for (int j = i; j <= n; ++j)
                f[j] += f[j-i];
    
        if (a <= 1)
            printf("%lld\n", ans);
        else
            printf("%lld\n", ans - f[n]);
    }
    
    void init()
    {
       int num = sscanf(str, "%d %d %d", &n, &a, &b);
        
       if (a > 300) a = 300;
       if (b > 300) b = 300;
    
       if (num == 1)
           a = 1, b = 300;
       else if (num == 2)
           b = a, a = 1;
    }
    
    int main()
    {
        while (gets(str))
        {
            init();
            solve();
        }
        return 0;
    }

     

    -------------------------------------------------------

    kedebug

    Department of Computer Science and Engineering,

    Shanghai Jiao Tong University

    E-mail: kedebug0@gmail.com

    GitHub: http://github.com/kedebug

    -------------------------------------------------------

  • 相关阅读:
    FreeCodeCamp:Chunky Monkey
    FreeCodeCamp:Slasher Flick
    FreeCodeCamp:Truncate a string
    FreeCodecamp:Repeat a string repeat a string
    FreeCodeCamp:Confirm the Ending
    FreeCodeCamp:Return Largest Numbers in Arrays
    FreeCodeCamp:Title Case a Sentence
    git和GItHub的区别
    dedecms简介
    html 7大知识点
  • 原文地址:https://www.cnblogs.com/kedebug/p/2781033.html
Copyright © 2020-2023  润新知