HDU 2152 Fruit( DP ) Fruit

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3514    Accepted Submission(s): 1998


Problem Description
转眼到了收获的季节,由于有TT的专业指导,Lele获得了大丰收。特别是水果,Lele一共种了N种水果,有苹果,梨子,香蕉,西瓜……不但味道好吃,样子更是好看。

于是,很多人们慕名而来,找Lele买水果。

甚至连大名鼎鼎的HDU ACM总教头 lcy 也来了。lcy抛出一打百元大钞,"我要买由M个水果组成的水果拼盘,不过我有个小小的要求,对于每种水果,个数上我有限制,既不能少于某个特定值,也不能大于某个特定值。而且我不要两份一样的拼盘。你随意搭配,你能组出多少种不同的方案,我就买多少份!"

现在就请你帮帮Lele,帮他算一算到底能够卖出多少份水果拼盘给lcy了。

注意,水果是以个为基本单位,不能够再分。对于两种方案,如果各种水果的数目都相同,则认为这两种方案是相同的。

最终Lele拿了这笔钱,又可以继续他的学业了~
 
Input
本题目包含多组测试,请处理到文件结束(EOF)。
每组测试第一行包括两个正整数N和M(含义见题目描述,0<N,M<=100)
接下来有N行水果的信息,每行两个整数A,B(0<=A<=B<=100),表示至少要买该水果A个,至多只能买该水果B个。
 
Output
对于每组测试,在一行里输出总共能够卖的方案数。
题目数据保证这个答案小于10^9
 
Sample Input
2 3
1 2
1 2
3 5
0 3
0 3
0 3
 
Sample Output
2
12

dp[i][j]表示用前i种水果弄到j个水果的组合数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
typedef pair<int,int>pii;
#define X first
#define Y second
const int oo = 1e9+7;
const double PI = acos(-1.0);
const double eps = 1e-6 ;
const int N = 305 ;
const int mod = 1e9+7;
LL dp[N][N];
int n , m , a[N] , b[N] ;
void init() {
    memset( dp , 0 , sizeof dp );
    dp[0][0] = 1 ;
    for( int i = 1 ; i <= n ; ++i ) {
        for( int j = 0 ; j <= m ; ++j ){
            for( int k = a[i] ; k <= b[i] ; ++k ){
                if( j < k ) continue ;
                dp[i][j] += dp[i-1][j-k];
            }
        }
    }
}
void Run() {
    for( int i = 1 ; i <= n ; ++i ){
        cin >> a[i] >> b[i] ;
    }
    init(); cout << dp[n][m] << endl ;
}
int main()
{
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    while( cin >> n >> m ) Run();
}
View Code