【2018 ICPC焦作网络赛 K】Transport Ship(多重背包二进制优化)

本文探讨了一种优化的01背包算法,用于解决特定条件下的船舶装载问题。面对N种不同载重能力的船只和大量询问,通过巧妙地合并同类船只并采用多重背包算法,大幅降低了原始时间复杂度,高效求解了如何安排船只以达到指定总重量的方案数量。

There are different kinds of transport ships on the port. The ith kind of ship can carry the weight of V[i]V[i] and the number of the ith kind of ship is 2c[i]-12^{C[i]} - 12. 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.

Input

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 lines, each line contains two integers: V[i](1≤V[i]≤20), C[i](1C[i]≤20), representing the weight the ith kind of ship can carry, and the number of the ith 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.

Output

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 10000000071000000007.

样例输入
1
1 2
2 1
1
2
样例输出
0
1

题意:

有N种船只,每种船只的载货量为V[i](以下代码用w[i]表示),每种船只的数量为2^c[i]-1。接下来有Q次询问,每次问有多少种载货方式可以填满容量S。

思路:

如果用裸的01背包的话时间复杂度是O(N*2^c[i]*10000),显然会超时,但是我们可以把每一种船合并,比如船只有2n-1艘的话,就拆成20+21+22+...+2n-1,1~2n-1之中的任意一个数都可以由拆分出来的数组成,将所有合并后的结果进行一次多重背包即可。

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4;
const int mod=1e9+7;
typedef long long ll;
int w[25],c[25];
ll dp[MAX+5];
int main()
{
    int n,i,T,q,s,j,k;
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        cin>>n>>q;
        for(i=1;i<=n;i++)
            cin>>w[i]>>c[i];
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(i=1;i<=n;i++)       //共n种船
        {
            int t=1;
            for(j=1;j<=c[i];j++)//每种船有2^c[i]-1只
            {
                for(k=MAX;k>=t*w[i];k--)
                    dp[k]=(dp[k]+dp[k-t*w[i]])%mod;
                t<<=1;
            }
        }
        while(q--)
        {
            cin>>s;
            cout<<dp[s]<<endl;
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/kannyi/p/9756877.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值