騰訊筆試:小Q的歌單

時間限制:1秒

空間限制:32768K

小Q有X首長度爲A的不同的歌和Y首長度爲B的不同的歌,現在小Q想用這些歌組成一個總長度正好爲K的歌單,每首歌最多隻能在歌單中出現一次,在不考慮歌單內歌曲的先後順序的情況下,請問有多少種組成歌單的方法。

輸入描述:

每個輸入包含一個測試用例。
每個測試用例的第一行包含一個整數,表示歌單的總長度K(1<=K<=1000)。
接下來的一行包含四個正整數,分別表示歌的第一種長度A(A<=10)和數量X(X<=100)以及歌的第二種長度B(B<=10)和數量Y(Y<=100)。保證A不等於B。

 

輸出描述:

輸出一個整數,表示組成歌單的方法取模。因爲答案可能會很大,輸出對1000000007取模的結果。

 

輸入例子1:

5
2 3 3 3

 

輸出例子1:

9
#include <map>  
#include <cmath>  
#include <queue>  
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <iostream>  
#include <algorithm> 
#include <sstream> 
#include <time.h> 
#include <vector>
#include <list>
#include <iostream>
#include<iomanip>
#include<time.h>  

#define N_MAX 205
#define INF (0x3f3f3f3f)
#define Q (1000000007)

using namespace std;

long long Data[201][1001];
int Length[201];
int main()
{
	int N, A, X, B, Y;
	cin >> N >> A >> X >> B >> Y;
	memset(Data, 0, sizeof(Data));
	for (int i = 1; i <= X; ++i)
	{
		Length[i] = A;
        Data[i][0] = 1;
	}
	for (int i = X + 1; i <= X + Y; ++i)
	{
		Length[i] = B;
        Data[i][0] = 1;
	}
    Data[0][0] = 1;
	for (int i = 1; i <= X + Y; ++i)
	{
		for (int j = 0; j <= N; ++j)
		{
			//當大於該商品價值時,可能性爲不選擇該商品的值與選擇該商品的值的和
			if (j >= Length[i])
				Data[i][j] = (Data[i - 1][j] % Q + Data[i - 1][j - Length[i]] % Q)% Q;
			else
				Data[i][j] = Data[i - 1][j] % Q;
		}
	}
	cout << Data[X + Y][N] << endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章