POJ #1221 - NIMODAL PALINDROMIC DECOMPOSITIONS

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

转载于:https://www.cnblogs.com/tonix/p/3816428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值