【source】
2018 Jiaozuo network game
Reference [blog]
https://blog.csdn.net/baymax520/article/details/82719454
[Title] Italy
There are N kinds of boats, cargo capacity of each vessel is v [i], each vessel has 2 ^ c [i] -1 species, there are times query q, each asked how many ways to fill the cargo capacity s .
[Thinking]
If 01 backpack bare, then the time complexity is O (N * 2 ^ c [i] * 10000), clearly timeout, but we can put each vessel were combined, such as a vessel that 2 ^ x-1 boat (s) then it is split into 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... + 2 ^ (x-1), any of a number of 1 ~ 2 ^ x-1 are split may be formed out of several compositions, the result of all combined for a 01 backpack can be.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 typedef long long ll; 6 const int M = 1e4+5; 7 const int mod = 1e9+7; 8 ll dp[M],v[25],c[25],V; 9 int main() 10 { 11 int t,n,Q,s; 12 scanf("%d",&t); 13 while(t--){ 14 scanf("%d%d",&n,&Q); 15 for(int i=1;i<=n;i++) 16 scanf("%d%d",&v[i],&c[i]); 17 18 memset(dp,0,sizeof dp ); 19 dp[0] = 1 ; 20 for(int i=1;i<=n;i++){ 21 for(int k=0;k<c[i];k++){ 22 V = 1ll* v[i] << k ; 23 for(int j=M-1;j>=V;j--){ 24 dp[j] += dp[j-V]; 25 dp[j] %= mod ; 26 } 27 } 28 } 29 for(int i=1;i<=Q;i++){ 30 scanf("%d",&s); 31 printf("%lld\n",dp[s]); 32 } 33 } 34 return 0; 35 }