Transport Ship【多重背包】

Transport Ship
时间限制: 1 Sec 内存限制: 128 MB
提交: 175 解决: 65
[提交] [状态] [命题人:admin]
题目描述
There are N different kinds of transport ships on the port. The i^th kind of ship can carry the weight of V[i] and the number of the ith kind of ship is 2C[i]-1. How many different schemes there are if you want to use these ships to transport cargo with a total weight of S? It is required that each ship must be full-filled. Two schemes are considered to be the same if they use the same kinds of ships and the same number for each kind.

输入
The first line contains an integer T(1≤T≤20), which is the number of test cases.
For each test case:
The first line contains two integers: N(1≤N≤20), Q(1≤Q≤10000), representing the number of kinds of ships and the number of queries.
For the next N lines, each line contains two integers: V[i] (1≤V[i]≤20), C[i] (1≤C[i]≤20), representing the weight the i^th kind of ship can carry, and the number of the i^th kind of ship is 2C[i]-1.
For the next Q lines, each line contains a single integer: S (1≤S≤10000), representing the queried weight.

输出
For each query, output one line containing a single integer which represents the number of schemes for arranging ships. Since the answer may be very large, output the answer modulo 1000000007.

样例输入
复制样例数据
1
1 2
2 1
1
2
样例输出
0
1

题目大意:
T T T组测试数据,对于每组测试数据,先输入两个整数 N , Q N,Q N,Q,其下 N N N行每行输入两个整数 V [ i ] , C [ i ] V[i],C[i] V[i],C[i],代表容量为 V [ i ] V[i] V[i]的船共有 2 C [ i ] − 1 2^{C[i]}-1 2C[i]1艘,再往后 Q Q Q行,代表有 Q Q Q组查询,每行输入一个数字 V V V,问运输体积为 V V V的货物共有多少种方案。

解题思路:
每种船的个数固定,运送固定体积,很明显是一道背包问题,不过此时的 d p [ i ] dp[i] dp[i]数组代表的是运送体积为 i i i时的方案数。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
ll dp[10100];
ll V[10010],C[10010];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(0),cin.tie(0);
    int T;
    scanf("%d",&T);
    while(T--) {
    	int N,Q;
    	scanf("%d %d",&N,&Q);
    	rep(i,1,N) {
    		scanf("%lld %lld",&V[i],&C[i]);
    	}
    	ms(dp);
    	dp[0]=1;
    	for(int i=1;i<=N;i++) {
    		if(V[i]*((1LL<<C[i])-1)>=10000) {
    			for(ll j=V[i];j<=10000;j++) {
    				dp[j]=(dp[j-V[i]]+dp[j])%mod;
    			}
    		}else{
	    		for(int k=0;k<C[i];k++) {
	    			for(ll j=10000;j>=(1LL<<k)*V[i];j--) {
	    				dp[j]=(dp[j]+dp[j-(1LL<<k)*V[i]])%mod;
	    			}
	    		}
    		}
    	}
    	//for(int i=1;i<=10;i++) cout<<dp[i]<<endl;
    	while(Q--) {
    		int s;
    		scanf("%d",&s);
    		printf("%lld\n",dp[s]);
    	}
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值