DP:POJ 3046 Ant Counting

Source: Internet
Author: User

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 <= $) families of ants which she labeled 1..T (A ants altogether). Each family had some number Ni (1 <= ni <=) 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 is 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,3} {2,2} {2,3}
5 Sets with 3 ants: {1,1,2} {1,1,3} {1,2,2} {+-*} {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-integer that's an ant type Present in the Hive
Output Line 1:the Number of sets of size S.. B (inclusive) that can is created. A set like {same} is the, the set {2,1} and should not being 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 the members; 5 more sets of ants with three members

Ideas:
DP[I][J] Indicates the number of species of J ants selected in the first I family;
then the backpack;

 #include <cstdio> #include <cstring> #include <iostream> using namespace
Std
const int mod=1000000;
    int fam[1009],dp[2][100009];//Scroll array int main () {int t,a,s,b;
    cin>>t>>a>>s>>b;
        for (int i=1;i<=a;i++) {int te;
        scanf ("%d", &te);
    fam[te]++;
    The dp[0][0]=1;//collection is empty when there is a case int total=0;
        int cur=0,pre=1;//represents a scrolling array long long ans=0; for (int i=1;i<=t;i++) {cur^=1,pre^=1; total+=fam[i];//The total number of ants in the first I family memset (dp[cur],0,sizeof dp[cur]);//Remember to clear 0 for (int j=0;j<=fam[i];j++)//forward I -Add the first Geometry element {for (int k=total;k>=j;k--)//backpack Package {dp[cur][k) to the set of 1 family
            ]= (Dp[cur][k]+dp[pre][k-j])%mod;
    }}} for (int i=s;i<=b;i++) ans= (ans+dp[cur][i])%mod;

    printf ("%lld\n", ans);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.