Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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", n, dp[n][1]);
    }
    return 0;
}
View Code
posted on 2014-06-30 13:16  Tonix  阅读(164)  评论(0编辑  收藏  举报