Transport Ship(多重揹包+方案數)

                                                   問題 K: Transport Ship

                                                                    時間限制: 1 Sec  內存限制: 128 MB

題目描述

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

 

輸入

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

輸出

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

 

樣例輸入

複製樣例數據

1
1 2
2 1
1
2

樣例輸出

0
1

題目描述:有T組數據,有n條船,q個詢問,接下來n行是每條船能裝多少重量w和數量c( 2^c-1)下邊q行詢問的貨物重量,問有多少種方案去裝貨物,每條船必需裝滿;

#include <bits/stdc++.h>
using namespace std;

int vec[10010];//用STL中的vector時間太長,原因可能是會多次訪問vector中的值
int dp[10010],g[10010];
const int inf=0x3f3f3f3f;
const int mod=1e9+7;

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,mu;
        int p=0;
        scanf("%d%d",&n,&mu);
        for(int i=0; i<n; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            b=(1<<b)-1;

            for(int k=1; k<=b; k=k*2)
            {
                b-=k;
                vec[p++]=a*k;
                //vec.push_back(a*k);
            }
            if(b>0)
               // vec.push_back(a*b);
               vec[p++]=a*b;
        }
        memset(dp,0,sizeof(dp));
        dp[0]=1;

        for(int j=0; j<p; j++)
            for(int k=10000; k>=vec[j]; k--)
            {
                if(dp[k-vec[j]]>0)
                    dp[k]+=dp[k-vec[j]];
                if(dp[k]>mod)
                    dp[k]-=mod;
            }


        for(int i=0; i<mu; i++)
        {
            int m;
            scanf("%d",&m);
            printf("%d\n",dp[m]);

        }
    }

    return 0;
}

 

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