组合学——糖果

Combinatorics - The Candies

本文关键字:糖果 组合      更新时间:2023-10-16

我在一次比赛中发现了这个问题,到现在还没能想出解决办法。

这个男孩有苹果,并放在盒子里。一个盒子里不能超过N/2个。他有多少种方法把糖果放进盒子里。

所以我要做的是用DP实现解决方案。下面是我的代码:

#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <vector>
#define size 1002
using namespace std;
long long a[size][size];
int n, k;
int main()
{
    cin >> n >> k;
    int kk = n/2;
    for(int i = 0; i <= k; ++i)
        a[0][i] = 1;
    a[0][0] = 0;
    for(int i = 0; i <= kk; ++i)
        a[i][1] = 1;
    for(int i = 1; i <= n; ++i) {
        for(int j = 2; j <= k; ++j) {
            int index = 0;
            long long res = 0;
            while(1) {
                res += a[i-index][j - 1];
                index += 1;
                if(index == kk + 1 || i-index < 0)
                    break;
            }
            a[i][j] = res;
        }
    }
    cout << a[n][k] << endl;
}

但问题是我们有大量的输入,如:

2≤N≤1000为糖果的数量,N为偶数;2≤S≤1000 -为小盒子的数量。

所以,对于像N = 1000和S = 1000这样的输入,我必须花费5*10^8个操作。这些数字非常大,所以我必须使用BigInteger算法?

也许有算法可以在线性时间内实现这个问题?谢谢,对不起我的英语!

您可以通过以下观察轻松地将时间复杂度从O(kn^2)降低到O(nk):

for(int i = 1; i <= n; ++i) {
    for(int j = 2; j <= k; ++j) {
        int index = 0;
        long long res = 0;
        while(1) {
            res += a[i-index][j - 1];
            index += 1;
            if(index == kk + 1 || i-index < 0)
                break;
        }
        a[i][j] = res;
    }
}
对于每个a[i][j],我们可以很容易地看到

a[i][j] = sum a[k][j - 1]与k从(i - n/2)i

因此,如果我们创建一个数组sum来存储前一步中所有索引的和,我们可以从上面的嵌套循环 中减少一个for循环。

a[i][j] = sum[i] - sum[i - (n/2) - 1];

伪代码:

long long sum[n + 1];
for(int j = 2; j <= k; ++j) {
    long long nxt[n + 1];
    for(int i = 1; i <= n; ++i) {
        int index = 0;
        long long res = sum[i] - sum[i - (n/2) - 1];
        a[i][j] = res;
        nxt[i] = nxt[i - 1] + a[i][j];//Prepare the sum array for next step
    }
    sum = nxt;
}

注意:以上代码没有处理数组sum的初始化步骤,也没有处理i 更新:

我下面的Java解决方案通过使用类似的想法被接受:

public static void main(String[] args) throws FileNotFoundException {
    // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
    // "output.txt")));
    PrintWriter out = new PrintWriter(System.out);
    Scanner in = new Scanner();
    int n = in.nextInt();
    int s = in.nextInt();
    BigInteger[][] dp = new BigInteger[n + 1][2];
    BigInteger[][] count = new BigInteger[2][n + 1];
    int cur = 1;
    for (int i = 0; i <= n / 2; i++) {
        dp[i][0] = BigInteger.ONE;
        count[0][i] = (i > 0 ? count[0][i - 1]  : BigInteger.ZERO)
                .add(dp[i][0]);
    }
    for (int i = n / 2 + 1; i <= n; i++) {
        dp[i][0] = BigInteger.ZERO;
        count[0][i] = count[0][i - 1];
    }
    for (int i = 2; i <= s; i++) {
        for (int j = 0; j <= n; j++) {
            dp[j][cur] = dp[j][1 - cur].add((j > 0 ? count[1 - cur][j - 1]
                    : BigInteger.ZERO)
                    .subtract(j > n / 2 ? count[1 - cur][j - (n / 2) - 1]
                            : BigInteger.ZERO));
            count[cur][j] = (j > 0  ? count[cur][j - 1] : BigInteger.ZERO)
                    .add(dp[j][cur]);
        }
        cur = 1 - cur;
    }
    out.println(dp[n][1 - cur]);
    out.close();
}