1. 程式人生 > >計蒜客 Transport Ship(ACM-ICPC 2018 焦作賽區網路預賽 K)(多重揹包裝滿的方案數)

計蒜客 Transport Ship(ACM-ICPC 2018 焦作賽區網路預賽 K)(多重揹包裝滿的方案數)

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[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)V[i](1≤V[i]≤20),C[i](1≤C[i]≤20), representing the weight the i^{th}ith kind of ship can carry, and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[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

題目來源

題意:多重揹包裝滿的方案數

解題思路:直接上多重揹包模板,然後改改遞推方程即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXN=2000005;
ll MOD = 1000000007;

ll W;
ll num[100005];
ll weight[100005];
ll dp[100005];
 
//01揹包模板
void zero_one_pack(ll w){
    for(ll i=W;i>=w;i--)
        dp[i]=(dp[i]+dp[i-w])%MOD;//關鍵是這個轉移,還是很好理解的
}
 
//多重揹包模板
void multi_pack(ll w,ll n){
    //否則將該物品分解成幾個小物品,然後用01揹包去求解。詳見揹包九講
    ll k=1;
    while(k<n){
        zero_one_pack(k*w);
        n-=k;
        k*=2;
    }
    zero_one_pack(n*w);
}
 
 
int main(){
 
 	int T;
 	scanf("%d",&T);
 	while(T--){
 		W=10000;
 		ll N,Q;
 		scanf("%lld%lld",&N,&Q);
 		for(ll i=1;i<=N;i++){
            scanf("%lld",&weight[i]);
            scanf("%lld",&num[i]);
            num[i]=(1<<num[i])-1;
        }
        
        memset(dp,0,sizeof(dp));
 		dp[0]=1;
        //對於每一個物品,用多重揹包求解
        for(ll i=1;i<=N;i++)
            multi_pack(weight[i],num[i]);

 		int tmp;
 		for(ll i=0;i<Q;i++){
 			scanf("%d",&tmp);
 			printf("%lld\n",dp[tmp]%MOD);
 		}
 	}

    return 0;
}