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

16 篇文章 0 订阅
11 篇文章 0 订阅

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<bits/stdc++.h>
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;
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值