首页 > 代码库 > UVA10616 - Divisible Group Sums(dp)

UVA10616 - Divisible Group Sums(dp)

UVA10616 - Divisible Group Sums(dp)

题目链接

题目大意:N个数,选择m个数出来,问相加的和能够整除MOD有多少种选择方式。

解题思路:从1到N数选择过去,每个数有选和不选两种可能,并且(num + d) % MOD = num % MOD + d % MOD, 所以可以这么做,最后判断一下余数等于0么。坑点是这题N个数会有负数,负数的取模 (num % MOD + MOD) % MOD.这一题因为没有控制选择的数目m的增加次数,导致re了好久,简直无语死了。。。

代码:

#include <cstdio>
#include <cstring>

typedef long long ll;
const int maxn = 205;
const int maxm = 15;
const int maxd = 25;

int N, Q, MOD, M;
int num[maxn], tmp[maxn];
ll f[maxn][maxm][maxd];

void init () {

    for (int i = 0; i < N; i++)
        scanf ("%d", &num[i]);    
}

ll dp (int n, int m, int d) {

    ll& ans = f[n][m][d];
    if (ans != -1)
        return ans;

    if (n == N) {

        if (m == M && d == 0)
            return ans = 1;
        return ans = 0;
    }

    ans = 0;
    if (m < M)
        ans += dp(n + 1, m + 1, (d + tmp[n]) % MOD);
    ans += dp(n + 1, m, d); 
    return ans;
}

int main () {

    int cas = 0;
    while (scanf ("%d%d", &N, &Q) && (N || Q)) {

        init();

        printf ("SET %d:\n", ++cas);
        for (int i = 0; i < Q; i++) {
            scanf ("%d%d", &MOD, &M);

            memset (f, -1, sizeof (f));
            for (int j = 0; j < N; j++)
                tmp[j] = (num[j] % MOD + MOD) % MOD;

            printf ("QUERY %d: %lld\n", i + 1, dp(0, 0, 0));
        }
    }
    return 0;
}

UVA10616 - Divisible Group Sums(dp)