Transport Ship【多重背包】
Transport Ship
時(shí)間限制: 1 Sec 內(nèi)存限制: 128 MB
提交: 175 解決: 65
[提交] [狀態(tài)] [命題人: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.
樣例輸入
復(fù)制樣例數(shù)據(jù)
1
1 2
2 1
1
2
樣例輸出
0
1
題目大意:
有TTT組測(cè)試數(shù)據(jù),對(duì)于每組測(cè)試數(shù)據(jù),先輸入兩個(gè)整數(shù)N,QN,QN,Q,其下NNN行每行輸入兩個(gè)整數(shù)V[i],C[i]V[i],C[i]V[i],C[i],代表容量為V[i]V[i]V[i]的船共有2C[i]?12^{C[i]}-12C[i]?1艘,再往后QQQ行,代表有QQQ組查詢(xún),每行輸入一個(gè)數(shù)字VVV,問(wèn)運(yùn)輸體積為VVV的貨物共有多少種方案。
解題思路:
每種船的個(gè)數(shù)固定,運(yùn)送固定體積,很明顯是一道背包問(wèn)題,不過(guò)此時(shí)的dp[i]dp[i]dp[i]數(shù)組代表的是運(yùn)送體積為iii時(shí)的方案數(shù)。
代碼:
#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_JUDGEfreopen("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; }總結(jié)
以上是生活随笔為你收集整理的Transport Ship【多重背包】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: UVA - 489 Han
- 下一篇: 【动态规划】简单背包问题II