[动态规划]Number Partition

Number Partition


Time Limit:1sMemory limit:32M
Accepted Submit:38Total Submit:175

A partition of a positive integer m into n parts is defined to construct a sequence a1,..,an such that a1+...+an=m, and a1<=a2<=...<=an.

It is apparent that such partition is not unique. We arrange them in lexicographic order. Your task is to find the k-th sequence.

Input

There are multiple test cases. Each case has only 1 line with 3 integers: m,n,k (1<=n<=10, 1<=m<=220). Please note that k can be very large but are always within the range.
Process to the end of file.

Output

For each case, output a line containing the k-th sequence.

Sample Input

9 4 3

Sample Output

1 1 3 4
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

long long seq[12][222][222];

int main()
{
    long long i, j, l, u, e;
    long long n, m, k;
    long long fix;

    for (i = 1; i <= 10; ++i)
    {
        for (j = i; j <= 220; ++j)
        {
            if (i == 1)
                seq[i][j][j] = 1;
            else
            {
                for (l = 1; l <= j / i; ++l)
                {
                    seq[i][j][l] = 0;
                    e = (j - l) / (i - 1);
                    for (u = l; u <= e; ++u)
                        seq[i][j][l] += seq[i - 1][j - l][u];
                }
            }
        }
    }

    while (cin >> n >> m >> k)
    {
        fix = 1;
        while (m >= 1)
        {
            if (m == 1)
                cout << n << endl;
            else
            {
                while (seq[m][n][fix] < k)
                {
                    k -= seq[m][n][fix];
                    ++fix;
                }
                n -= fix;
                cout << fix << ' ';
            }
            --m;
        }
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值