ACM-ICPC 2018 焦作網絡預賽K-Transport Ship(多重揹包)

ACM-ICPC 2018 焦作網絡預賽K-Transport Ship

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

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)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≤N≤20),Q(1≤Q≤10000)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 NNN lines, each line contains two integers: Vi,CiV[i](1 \le V[i] \le 20), C[i](1 \le C[i] \le 20)Vi,Ci, representing the weight the ithi^{th}ith kind of ship can carry, and the number of the ithi^{th}ith kind of ship is 2C[i]−12^{C[i]} - 12C[i]−1.

For the next QQQ lines, each line contains a single integer: S(1≤S≤10000)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 100000000710000000071000000007.
樣例輸入

1
1 2
2 1
1
2

樣例輸出

0
1

題目來源

ACM-ICPC 2018 焦作賽區網絡預賽

多重揹包求方案數

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int maxn = 25;
const int maxm = 10010;
int v[maxn],c[maxn];
ll dp[maxm];

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,q;
        scanf("%d%d",&n,&q);
        memset(dp,0,sizeof(dp));
        dp[0] = 1;
        for(int i = 1; i <= n; i++){
            scanf("%d%d",&v[i],&c[i]);
            ll cur = 1;
            for(int j = 1; j <= c[i]; j++){
                for(int k = 10000; k >= cur * v[i]; k--){
                    dp[k] = (dp[k] + dp[k-cur*v[i]]) % mod;
                }
                cur <<= 1;
            }
        }
        for(int i = 1; i <= q; i++){
            int s;
            scanf("%d",&s);
            printf("%lld\n",dp[s]);
        }
    }
    return 0;
}

多重揹包詳細講解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章