您的位置:首页 > 其它

POJ 3046 Ant Counting (dp)

2015-03-07 01:54 351 查看
Ant Counting

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3111 Accepted: 1233
Description

Bessie was poking around the ant hill one day watching the ants march to and fro while gathering food. She realized that many of the ants were siblings, indistinguishable from one another. She also realized the sometimes only one ant would go for food, sometimes
a few, and sometimes all of them. This made for a large number of different sets of ants! 

Being a bit mathematical, Bessie started wondering. Bessie noted that the hive has T (1 <= T <= 1,000) families of ants which she labeled 1..T (A ants altogether). Each family had some number Ni (1 <= Ni <= 100) of ants. 

How many groups of sizes S, S+1, ..., B (1 <= S <= B <= A) can be formed? 

While observing one group, the set of three ant families was seen as {1, 1, 2, 2, 3}, though rarely in that order. The possible sets of marching ants were: 

3 sets with 1 ant: {1} {2} {3} 

5 sets with 2 ants: {1,1} {1,2} {1,3} {2,2} {2,3} 

5 sets with 3 ants: {1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3} 

3 sets with 4 ants: {1,2,2,3} {1,1,2,2} {1,1,2,3} 

1 set with 5 ants: {1,1,2,2,3} 

Your job is to count the number of possible sets of ants given the data above. 

Input

* Line 1: 4 space-separated integers: T, A, S, and B 

* Lines 2..A+1: Each line contains a single integer that is an ant type present in the hive
Output

* Line 1: The number of sets of size S..B (inclusive) that can be created. A set like {1,2} is the same as the set {2,1} and should not be double-counted. Print only the LAST SIX DIGITS of this number, with no leading zeroes or spaces.
Sample Input
3 5 2 3
1
2
2
1
3

Sample Output
10


多重集组合数  dp[i][j] = dp[i][j] + dp[i-1][j-k],  0<=k<=min(j,a[i])

优化为 dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1[[j-1-a[i]]

AC代码如下:

//
// POJ 3046 Ant Counting
//
// Created by TaoSama on 2015-03-05
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e6;
const int N = 1e5 + 10;

int n, m, l, r, dp[2]
, w[1005];

int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

while(cin >> n >> m >> l >> r) {
memset(w, 0, sizeof w);
for(int i = 1; i <= m; ++i) {
int x; cin >> x;
++w[x];
}
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 1; i <= n; ++i) {
memset(dp[i & 1], 0, sizeof dp[i & 1]);
for(int k = 0; k <= w[i]; ++k) {
for(int j = k; j <= m; ++j) {
dp[i & 1][j] = (dp[i & 1][j] + dp[(i - 1) & 1][j - k]) % MOD;
}
}
}
int ans = 0;
// for(int i = 1; i <= m; ++i)
// cout << dp[n & 1][i] << endl;
for(int i = l; i <= r; ++i)
ans = (ans + dp[n & 1][i]) % MOD;
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: