ACM-ICPC 2018 焦作赛区网络预赛 Transport Ship—— 二进制优化的01背包

There are NN different kinds of transport ships on the port. The i^{th}i
th
kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}i
th
kind of ship is 2^{C[i]} - 12
C[i]
−1. How many different schemes there are if you want to use these ships to transport cargo with a total weight of SS?

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 \le T \le 20)T(1≤T≤20), which is the number of test cases.

For each test case:

The first line contains two integers: N(1 \le N \le 20), Q(1 \le Q \le 10000)N(1≤N≤20),Q(1≤Q≤10000), representing the number of kinds of ships and the number of queries.

For the next NN lines, each line contains two integers: V[i](1 \le V[i] \le 20), C[i](1 \le C[i] \le 20)Vi,Ci, representing the weight the i^{th}i
th
kind of ship can carry, and the number of the i^{th}i
th
kind of ship is 2^{C[i]} - 12
C[i]
−1.

For the next QQ lines, each line contains a single integer: S(1 \le S \le 10000)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
题目链接:https://nanti.jisuanke.com/t/31720
题意:
t组样例,n种船只,q个询问,接下来n行给你每种船只的信息:wei[i]表示这个船只的载重,c[i]表示这种船只有 2 c [ i ] 1
那么我们就可以用二进制优化来做,既然是 2 c 1 ,那么就是 2 0 + 2 1 + 2 2 + . . . + 2 c 1 ,存在wei里面
接下来就是背包了,dp[i][j]表示拿到第i个wei的时候到达j有多照中方法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=(int)1e9+7;
const int maxn=1005;
int dp[500][10005];
int wei[1005],n,q,s;
int ar;
int main(){
    int t;
    cin>>t;
    while(t--){
        ar=0;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++){
            int v,c;
            scanf("%d%d",&v,&c);
            int ba=1;
            while(c--){
                wei[++ar]=ba*v;
                ba*=2;
            }
        }
        dp[0][0]=1;
        for(int i=1;i<=ar;i++){
            for(int j=0;j<=10000;j++){
                if(j<wei[i]){
                    dp[i][j]=dp[i-1][j];
                }
                else{
                    dp[i][j]=(dp[i-1][j]+dp[i-1][j-wei[i]])%mod;
                }
            }
        }
        while(q--){
            int S;scanf("%d",&S);
            printf("%d\n",dp[ar][S]);

        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/82716691