HDU 4248 A Famous Stone Collector(DP + 组合数)

题目链接:点击打开链接

思路:DP + 组合数。 用d[i][j]表示前第i种颜色的石头, 已经用了j个的方法数, 每次枚举第i种石头放多少个, 假设放k个, 那么相当于从j个位置中选k个, 预处理组合数就行了。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 100 + 10;
int T,n,m,kase = 0,a[maxn];
ll d[2][maxn*maxn];
ll C[maxn*maxn][maxn];
inline void add(ll &a, ll b) {
    a += b;
    if(a >= mod) a -= mod;
}
void init() {
    for(int i = 0; i <= 10000; i++) {
        C[i][0] = 1;
        for(int j = 1; j <= min(i, 100); j++) {
            C[i][j] = C[i-1][j-1] ;
            add(C[i][j], C[i-1][j]);
        }
    }
}
int main() {
    init();
    while(~scanf("%d",&n)) {
        for(int i = 1; i <= n; i++) scanf("%d",&a[i]);

        int u = 1;
        memset(d[u], 0, sizeof(d[u]));
        d[u][0] = 1;
        int maxlen = 0;
        for(int i = 1; i <= n; i++) {
            maxlen += a[i];
            memset(d[u^1], 0, sizeof(d[u^1]));
            for(int j = 0; j <= maxlen; j++) {
                for(int k = 0; k <= a[i]; k++) {
                    if(k > j || d[u][j-k] == 0) continue;
                    ll cur = C[j][k] * d[u][j-k];
                    if(cur >= mod) cur %= mod;
                    add(d[u^1][j] , cur);
                }
            }
            u ^= 1;
        }
        ll ans = 0;
        for(int i = 1; i <= maxlen; i++) {
            add(ans , d[u][i]);
        }
        printf("Case %d: %I64d\n",++kase, ans);
    }
    return 0;
}


你可能感兴趣的:(dp,HDU,ACM-ICPC,组合数)