1. 程式人生 > >Poj 3046(dp)

Poj 3046(dp)

Ant Counting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1954 Accepted: 822

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

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

書上例題,多重集組合數,需要對遞推式優化,類似完全揹包的優化。雖然是10^8,注意取模用減法, 也是可以過的。
#include<cstdio>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int maxn = 1000 + 5;
const int maxm = 100000 + 5;
const int INF = 1000000000;
const int Mod = 1000000;

int t,a,s,b;
int dp[2][maxm];
int sum[maxn];

void solve(){
    int ans = 0;
    for(int i = 0;i < maxm;i++) dp[0][i] = 0;
    dp[0][0] = dp[1][0] = 1;
    for(int i = 1;i <= t;i++){
        for(int j = 1;j < maxm;j++){
            if(j-sum[i]-1 >= 0){
                dp[i&1][j] = dp[(i-1)&1][j] + dp[i&1][j-1] - dp[(i-1)&1][j-sum[i]-1] + Mod;
                while(dp[i&1][j] >= Mod) dp[i&1][j] -= Mod;
            }
            else {
                dp[i&1][j] = dp[(i-1)&1][j] + dp[i&1][j-1];
                while(dp[i&1][j] >= Mod) dp[i&1][j] -= Mod;
            }
        }
    }
    for(int i = s;i <= b;i++){
        ans = ans + dp[t&1][i];
        while(ans >= Mod) ans -= Mod;
    }
    printf("%d\n",ans);
}

int main(){
    while(scanf("%d%d%d%d",&t,&a,&s,&b) != EOF){
        memset(sum,0,sizeof(sum));
        for(int i = 0;i < a;i++){
            int x;scanf("%d",&x);
            sum[x]++;
        }
        solve();
    }
    return 0;
}


相關推薦

Poj 3046dp

Ant Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1954 Accepted: 822 Description Bessie was poking around the

poj——1080dp

解析:dp[i][j]來由: 1.dp[i][j-1]轉移,則‘-’和s2[j]匹配。 2.dp[i-1][j]轉移,則s1[i]和‘-’匹配。 3.dp[i-1][j-1],則s1[i]和s2[j

POJ 1260-PearlsDP

ctype set lowest cas str pri font mount scan Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7465 Accepted

poj - 1088 - 滑雪dp

target art dsm 題目 ipp 每次 元素 org mod 題意:一個R * C的矩陣(1 <= R,C <= 100),元素代表該點的高度h(0<=h<=10000),從隨意點出發,每次僅僅能走上、下、左、右。且將要到的高度要比

POJ 題目1157 LITTLE SHOP OF FLOWERSDP

pop include ffffff suppose produce str tween cin 0ms LITTLE SHOP OF FLOWERS Time Limit: 1000MS Memory Limit: 10000K

POJ 1458DP初步_B題解題報告

con lap %d org for opened long hid body 題目鏈接:http://poj.org/problem?id=1458 -------------------------------------------------------- 題意:給

POJ 1321DP初步_I題解題報告

org sin return 題意 存在 分享圖片 problem aps 題目 題目鏈接:http://poj.org/problem?id=1321 -------------------------------------------------------- 題意:

H - Increasing Sequences POJ - 1239 dp好題

H - Increasing Sequences POJ - 1239 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as to minimize th

hdu 1081/poj 1050 最大子矩陣和dp

換成谷歌瀏覽器以後終於可以黏貼程式碼了,更新以後的markdown真心難用。。。 dp問題,先把給定的二維矩陣壓縮,變成一維矩陣,如此即可變成hdu1003,用動態規劃的思路求解即可 #include<iostream> #include<cm

POJ 3356 AGTCdp x串變成y串最小代價

題目:http://poj.org/problem?id=3356 題意: 給你x串和y串,讓求操作x串變成y串的最小次數 操作:插入、刪除、修改一個字元 思路:用dp[i][j]表示x的前i個字元變成y的前j個字元的最小次數 修改 dp[i][j] = dp[i-1][j-1]+1

POJ 1925】 Spidermandp

Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6806 Accepted: 1361 Description Dr. Octopus kidnapped Spiderman's girlf

POJ 2029】 Get Many Persimmon TreesDP

Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4024 Accepted: 2628 Description Seiji Hayashi had been a professor of t

poj——1159dp之最長公共子序列

注:C++執行時Runtime Error,G++過了。(這編譯器,真無語了)。 #include <iostream> #include <cmath> #include

POJ Cow Bowlingdp

Description The cows don’t use actual bowling balls when they go bowling. They each take a numb

POJ 1159dp+備忘錄

不用short會引起記憶體超限, 不用備忘錄方法會引起TLE 但基本思路還是如下: 遞迴求解。 也可以用動態規劃演算法,但我看了半天還是不好理解,都說的不是特別好,要是有哪位大牛有好的思路希望在評論區給予我幫助,感激不盡。 #include<io

poj 2904 The Mailboxes Manufacturers ProblemDP

The Mailboxes Manufacturers Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 947 Accepted: 685 Description In the

poj 3176 dp 金字塔

Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17271 Accepted: 11531 Description The cows don't use actual

POJ 2192 Zipper dp

 連結: http://poj.org/problem?id=2192  題意:就是給定三個字串A,B,C;判斷C能否由AB中的字元組成,同時這個組合後的字元順序必須是A,B中原來的順序,不能逆序;例

POJ 2581 Exact Change Onlydp

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibodeaux groggily yawned.  "Not in Vegas, I

poj 滑雪DFS||DP+遞迴

Description Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區域中最長底滑坡。區域由一個二維陣列給出。陣列的每個數字代表點的高度。下面是一個例子  1 2