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
  • 相关阅读:
    volley框架使用
    Insert Interval
    candy(贪心)
    Best Time to Buy and Sell Stock
    Best Time to Buy and Sell Stock III
    distinct subsequences
    edit distance(编辑距离,两个字符串之间相似性的问题)
    trapping rain water
    word break II(单词切分)
    sudoku solver(数独)
  • 原文地址:https://www.cnblogs.com/tonix/p/3816428.html
Copyright © 2011-2022 走看看