问题 K: Transport Ship
时间限制: 1 Sec 内存限制: 128 MB
提交: 124 解决: 39
[提交] [状态] [命题人: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 思路与该题类似https://blog.youkuaiyun.com/qq_42936517/article/details/84961661
不过本题是多重背包,那道题是完全背包
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 30;
const int maxn = 1e6 + 5;
const ll mod = 1e9 + 7;
int T, q, n, cnt, s;
int c[N], v[N];
ll dp[maxn], a[maxn];
ll ksm(ll aa, ll bb) {
ll ans = 1;
while(bb) {
if(bb & 1) ans = ans * aa;
aa = aa * aa;
bb >>= 1;
}
return ans;
}
void bitt() {
cnt = 1;
for(int i = 1; i <= n; i++) {
ll tmp = v[i];
ll cc = c[i] - 1;
while(cc >= 0) {
a[cnt++] = tmp * ksm(2, cc);
cc--;
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d %d", &n, &q);
for(int i = 1; i <= n; i++) {
scanf("%d %d",&v[i], &c[i]);
}
bitt();
for(int i = 0; i <= 10000+10; i++) dp[i] = 0;
dp[0] = 1;
for(int i = 1; i < cnt; i++) {
for(int j = 10000; j >= a[i]; j--) {
dp[j] = (dp[j] + dp[j - a[i]]) % mod;
}
}
while(q--) {
scanf("%d", &s);
printf("%lld\n", dp[s] % mod);
}
}
return 0;
}
本文详细解析了一种特定的多重背包问题,通过实例阐述了如何使用动态规划算法求解不同类型的运输船装载货物的方案数。文章提供了完整的代码实现,并对比了与完全背包问题的异同,为读者深入理解并解决类似问题提供了有价值的参考。

被折叠的 条评论
为什么被折叠?



