POJ - 3046 Ant Counting (dp + 滚动数组)

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

题意:

      就是共有a只蚂蚁,分为 t 组,他们相互组合,分组成s - b组,问有多少中分法

ps.(1,2)和(2,1)是同一种分法,(1,1)也是一种分法

思路:

    拿样例做个推理:

    样例的蚂蚁数为5个,分为3个family , fa[1] = 2, fa[2] = 2, fa[3] = 1

先说一下

dp[i][j], i代表的是前i的数,j代表的是j的集合,dp[i][j]代表的是前i的家族所组成j的集合的种类数

先推1的能组成的种类数

i/j a
0 1
1 1
2 1
3 0

        对于0的集合,a家族只有1种方案,就是不派蚂蚁。

         而1的集合,a家族也只有1种方案,派1只蚂蚁

         而2的集合,a家族也只有1种方案,派2只蚂蚁

          3的集合的话,a家族没有方案,

i/j a b
0 1 1
1 1 2
2 1 3
3 0 2
4 0 1
5 0 0

而对于b家族,

0的集合同样1种方案

1的集合有派0只和1只两个方案

 2的集合

           b家族可以排除0只,1家族派2只,1种

           b家族派1只  ,1家族1只  , 1种

           b家族派2只,1家族0只,1种

      一共3种

3的集合 

      b家族不能派0只,

     1只的话,a家族只能派 2只,1种

      2只话,a家族只能派1只, 1种

     一共2种

4的集合

   b家族只能派2只,a家族也派2只

  一种

而c家族

i/j a b c
0 1 1 1
1 1 2 3
2 1 3 5
3 0 2 5
4 0 1 3
5 0 0 1
6 0 0 0

由此可以初步推断

当0的集合的话  c家族只能派0,而(a,b)也只能派0,dp[3][0]=dp[2][0]

1的集合的话,c家族可派0,1  ,而对应的(a,b)只能派1,0   dp[3][1] = dp[2][0] + dp[2][1]

2的集合的话 c家族可派0,1 对应的(a, b)派1,2  dp[3][2] = dp[2][1] + dp[2][2]

....

5的集合的话,c家族只能派1,对应的(a,b)为4, dp[3][5] = dp[2][4]

。。。。

由此可以简单看出,

 dp[i][j] = \sum (0\leq k\leq n[i]])dp[i][j - k]

AC 代码

//
//  main.cpp
//  G - Ant Counting
//
//  Created by dhl on 2018/7/17.
//  Copyright © 2018年 dhl. All rights reserved.
//

#include <iostream>
#include <string.h>
using namespace std;

int fa[1005];
int add[1005], sum[100005];
int dp[2][100005];
int main(int argc, const char * argv[]) {
    int t, a, s, b;
    cin >> t >> a >> s >> b;
    memset(fa, 0, sizeof(fa));
    for (int i = 1; i <= a; i ++) {
        int x;
        cin >> x;
        fa[x] ++;//每个家族的人数
    }
    for (int i = 1; i <= t; i ++) {
        add[i] = add[i - 1] + fa[i];//记录前i个家族的可形成的种人数
    }
    memset(dp, 0, sizeof(dp));
    int *pre = dp[0], *nex = dp[1];//两个指针记录前一个的数组和后一个的数组
    dp[0][0] = 1;//初始化0为1
    for (int i = 1; i <= t; i ++) {
        sum[0] = pre[0];//记录起始
        for (int j = 1; j <= add[i]; j ++) {
            sum[j] = (sum[j - 1] + pre[j]) % 1000000;//求前j个的和
        }
        for (int j = 0; j <= add[i]; j ++) {
            int temp = max(0, j - fa[i]);
            nex[j] = (temp == 0 ? sum[j] : (sum[j] - sum[temp - 1] + 1000000));
            nex[j] %= 1000000;
            //nex记录的是i家族在每一个j的种类数,当j < fa[i] 时就是每一项相加,而一旦超出这个fa[i],那么种类数就是sum[j] - sum[temp-1]了类比c家族
        }
        swap(nex, pre);//交换nex和pre,使数组滚动
    }
    int ans = 0;
    for (int i = s; i <= b; i ++) {
        ans = (ans + pre[i]) % 1000000;
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_jizhideqingwa/article/details/81086977
今日推荐