计蒜客 Transport Ship (2018-ACM-ICPC-焦作-网络赛)DP

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

题目来源

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


题意: 有 n 种不同种类的船,每种船有一个 v 载重值,船有 2的 c 次方 - 1 艘

现在有 m 次询问,每次询问给出一批货物的重量,问你有多少种安排方法把货物放到船上,且每艘船必须装满

思路: 多重背包,由于每艘船必须装满,并且询问的是重量,那么 dp[s] 表示载重为 s 的时候的方案数

那么对于 第 i 种船如果安排 ai 艘,使得总载重达到 k ,那么状态转移方程为:

dp[ k ] = ( dp[ k ] + dp[ k - ai * vi ] ) % mod  因为要统计方案数,所以直接加起来就可以了

因为船有 2 的 c 次方 - 1 艘,所以我们可以用二进制的倍增法

即枚举该种类的船的数量为 1 2 4 8 16 32 64 ... 艘,并且是从小到大倍增的话,后面的情况会与前面的融合,就可以搭配出 

使用了 1 到 ( 2 的 c 次方 - 1) 艘该种类的船的所有情况

#include
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))

const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;

ll dp[maxn];

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

 

你可能感兴趣的:(ACM-,DP,思维题)