码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-22 20:39:02      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:点击打开链接

思路: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;
}


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

标签:

原文地址:http://blog.csdn.net/weizhuwyzc000/article/details/51204360

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!