poj 3046 Ant Counting
Posted Jason杰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 3046 Ant Counting相关的知识,希望对你有一定的参考价值。
题目大意:
有编号一到t的蚂蚁家族,每个家族有不同的蚂蚁数。
问构成S只蚂蚁到构成B只蚂蚁共有多少种方式
例如
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}
Input
* Lines 2..A+1: Each line contains a single integer that is an ant type present in the hive
Output
Sample Input
3 5 2 3 1 2 2 1 3
Sample Output
10
这道题用母函数来求解。
所谓母函数,就是生成函数,说白了就是多项相乘,每一项分别表示每个家族里取一只,两只,三只。。。
最后乘出的结果中,x的n次方的系数就表示构成n个蚂蚁有多少种方式
代码如下
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #define M 1000000 5 int cnt[1002] = {0}; 6 int c1[100002] = {0}; 7 int c2[100002] = {0}; 8 int ans[100002] = {0}; 9 int n; 10 11 void multi(int cn1, int cn2) { 12 for(int i = 0; i <= cn1; i++) { 13 for(int j = 0; j <= cn2; j++) { 14 ans[i+j] = (ans[i+j] + c1[i]*c2[j])%M; 15 } 16 } 17 n = cn1 + cn2; 18 } 19 20 int main(int argc, char const *argv[]) 21 { 22 //freopen("input.txt","r",stdin); 23 int t, s, a, b; 24 scanf("%d %d %d %d",&t, &a, &s, &b); 25 for(int i = 0; i < a; i++) { 26 int tmp; 27 scanf("%d",&tmp); 28 cnt[tmp]++; 29 } 30 for(int i = 0; i <= cnt[1];i++) { 31 ans[i] = 1; 32 } 33 n = cnt[1]; 34 for(int i = 2; i <= t; i++) { 35 memset(c1, 0, sizeof(c1)); 36 memset(c2, 0, sizeof(c2)); 37 for(int j = 0; j <= n; j++) { 38 c1[j] = ans[j]; 39 } 40 for(int j = 0; j <= cnt[i]; j++) { 41 c2[j] = 1; 42 } 43 memset(ans, 0, sizeof(ans)); 44 multi(n,cnt[i]); 45 } 46 int ansm = 0; 47 for(int i = s; i <= b; i++) { 48 ansm = (ansm + ans[i])%M; 49 } 50 printf("%d\n",ansm); 51 return 0; 52 }
注意,此题要求输出后六位,要mod M
以上是关于poj 3046 Ant Counting的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3046 Ant Counting ( 多重集组合数 && 经典DP )
bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*