[动态规划]Number Partition

Number Partition


Time Limit:1s Memory limit:32M
Accepted Submit:38 Total 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;
}

猜你喜欢

转载自blog.csdn.net/micklongen/article/details/87702336