ACM-ICPC2018焦作网络赛 Transport Ship(二进制背包+方案数)

Transport Ship

  •  25.78%
  •  1000ms
  •  65536K
 

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(1T20), 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(1N20),Q(1Q10000), 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](1V[i]20),C[i](1C[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(1S10000), 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

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

 

 

#include<bits/stdc++.h>
#define MAX 105
#define MOD 1000000007
using namespace std;
typedef long long ll;

int v[MAX],c[MAX],a[2000005];
int two[MAX];
ll dp[10005];

void init(){
    two[1]=2;
    for(int i=2;i<=20;i++){
        two[i]=two[i-1]*2;
    }
}
int main()
{
    int t,n,q,V,i,j;
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&q);
        for(i=1;i<=n;i++){
            scanf("%d%d",&v[i],&c[i]);
            c[i]=two[c[i]]-1;
        }
        int cc=0;
        for(i=1;i<=n;i++){
            if(c[i]==0) continue;
            for(j=1;j<=c[i];j<<=1){
                cc++;
                a[cc]=j*v[i];
                c[i]-=j;
            }
            if(c[i]==0) continue;
            cc++;
            a[cc]=c[i]*v[i];
        }
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(i=1;i<=cc;i++){
            for(j=10000;j>=a[i];j--){
                dp[j]+=dp[j-a[i]];
                dp[j]%=MOD;
            }
        }
        while(q--){
            scanf("%d",&V);
            printf("%lld\n",dp[V]%MOD);
        }
    }
    return 0;
}

 

posted @ 2018-09-15 18:04  yzm10  阅读(347)  评论(0编辑  收藏  举报