Ant Counting
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
Hint
INPUT DETAILS:
Three types of ants (1..3); 5 ants altogether. How many sets of size 2 or size 3 can be made?
OUTPUT DETAILS:
5 sets of ants with two members; 5 more sets of ants with three members
Source
题目类型:线性DP
算法分析:dp[i][j]表示使用前i种蚂蚁构成尺寸为j的集合的个数,初始化dp[0][0] = 1,状态转移方程为dp[i][j] = Simga(dp[i-1][j-k]) k = 0->min(j, a[i]), a[i]为第i种蚂蚁的个数,将求和写成递推形式可得dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1-a[i]],由于蚂蚁总数比较大,所以需要使用滚动数组来实现,最后累加输出即可。注意相减取模时要先加上模值!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
/************************************************** filename :e.cpp author :maksyuki created time :2017/2/9 22:50:49 last modified :2017/2/10 9:44:38 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CPPFF ifstream cin ("in") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e6; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int cnt[maxn], dp[2][maxn]; int main() { //CFF; //CPPFF; int T, A, S, B; scanf("%d%d%d%d", &T, &A, &S, &B); memset(cnt, 0, sizeof(cnt)); memset(dp, 0, sizeof(dp)); dp[0][0] = 1; int tmp; for(int i = 1; i <= A; i++) { scanf("%d", &tmp); cnt[tmp]++; } for(int i = 1; i <= T; i++) { for(int j = 0; j <= B; j++) { dp[i&1][j] = dp[(i-1)&1][j]; if(j - 1 >= 0) dp[i&1][j] = (dp[i&1][j] + dp[i&1][j-1]) % MOD; if(j - 1 - cnt[i] >= 0) dp[i&1][j] = (dp[i&1][j] - dp[(i-1)&1][j-1-cnt[i]] + MOD) % MOD; } } int sum = 0; for(int i = S; i <= B; i++) sum = (sum + dp[T&1][i]) % MOD; printf("%d\n", sum); return 0; } |
- « 上一篇:poj1742
- hdu1398:下一篇 »