zoukankan      html  css  js  c++  java
  • POJ #1221

    http://hi.baidu.com/zldiablo/item/32f3e614de94f48d88a9560d
    Not very easy to identify the dp recurrence relation at first sight.. but strip your artificial thoughts, go back to natural: it has a sense of expansion. One note on initialization before actual DP: all impossible splitting assigned as 1. Think about 2.

    Details:
    1. You can only use unsigned __int64
    2. Use "%llu" to print out the uint64_t

    //    1221
    //    http://hi.baidu.com/zldiablo/item/32f3e614de94f48d88a9560d
    //    n is the input: the sum of all integers
    //    j is the end integer
    //    s[n][j] is the count of splitting, with sum of n, and end integer is NO LESS THAN j
    //    so, NO LESS THAN has two cases:
    //    1.    It is j        -> its count is s[n-2*j][j]
    //    2.    It is > j    -> its count is s[n][j+1]
    //    s[n][j] = s[n-2*j][j] + s[n][j+1] ..and s[n][1] is the expected
    //    
    //    recurrence direction: n is less2larger, j is larger2less
    
    #include <stdio.h>
    
    typedef unsigned __int64 uint64_t;
    
    const int MaxSize = 301;
    uint64_t dp[MaxSize][MaxSize];
    
    void precalc()
    {
        //    1. Init
        dp[1][1] = 1;
        //    All impossible splitting as 1, to start dp
        for (int i = 1; i <= MaxSize; i++)    
            dp[0][i] = 1;
    
        //    2. Go
        for (int i = 2; i <= MaxSize; i++)
        {
            //    All impossible splitting as 1, to start dp
            for (int j = i / 2 + 1; j <= i; ++j)    
                dp[i][j] = 1;
    
            //    Proceed
            for (int j = i / 2; j >= 1; j--)
                dp[i][j] = dp[i - 2 * j][j] + dp[i][j + 1];
        }
    }
    
    int main()
    {
        precalc();
    
        int n; 
        while (scanf("%d", &n), n != 0)
        {
            printf("%d %llu
    ", n, dp[n][1]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    SSH出现ls command not found
    SVN打包备份
    【转】Linux安装JDK1.7 prm
    任务
    java多线程
    JAVA开发中151个建议
    Linux Too Many OpenFiles
    【收藏】Linux tail命令
    Linux读取属性配置文件注意事项
    [转]Linux端口查看命令
  • 原文地址:https://www.cnblogs.com/tonix/p/3816428.html
Copyright © 2011-2022 走看看