A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was namedLL virus.
After several weeks of research, the scientists found the LL virus highly lethal and infectious. But more importantly, it has a long incubation period. Many victims were unaware of being infected until everything was too late. To prevent from the apocalypse, we need medicine!
Fortunately, after another several weeks of research, the scientists have finished the analysis of the LL virus. You need write a program to help them to produce the medicine.
The scientists provide you N kinds of chemical substances. For each substance, you can either use it exactWi milligrams in a medicine, or not use it. Each selected substance will addTi points of therapeutic effect value (TEV) to the medicine.
The LL virus has Q different variants. For each variant, you need design a medicine whose total weight equals toMi milligrams and total TEV equals to Si points. Since the LL virus is spreading rapidly, you should start to solve this problem as soon as possible!
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N (1 <= N <= 400) andQ (1 <= Q <= 400).
For the next N lines, each line contains two integers Wi (1 <=Wi <= 50) and Ti (1 <= Ti <= 200000).
Then followed by Q lines, each line contains two integers Mi (1 <=Mi <= 50) and Si (1 <= Si <= 200000).
For each test case, output Q lines. For the i-th line, output the indexes (1-based) of chemical substances in the i-th medicine, separated by a space. If there are multiple solutions, output any one. If there is no solution, output "No solution!" instead.
1 3 3 2 10 1 12 1 5 3 15 4 27 3 17
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define maxn 401 #define MAXN 200005 #define INF 0x3f3f3f3f #define mod 1000000007 #define pi acos(-1.0) #define eps 1e-6 typedef unsigned long long ull; using namespace std; int n,m,u,v,ma; int w[maxn],t[maxn]; short pre[200001][51]; ull dp[200001]; void solve() { int i,j; memset(pre,0,sizeof(pre)); memset(dp,0,sizeof(dp)); dp[0]=1; ma=200000; ull tmp,k,tot=(1ULL<<51)-1; for(i=1;i<=n;i++) { for(j=ma;j>=t[i];j--) { if(dp[j-t[i]]==0) continue ; tmp=dp[j]; dp[j]|=(dp[j-t[i]]<<w[i])&tot; for(k=tmp^dp[j];k;k=(k-1)&k) // 新增加的状态 一位一位取出 { pre[j][__builtin_ctzll(k)]=i; // builtin_ctzll-末尾有几个0 } } } } int main() { int i,j,test,tu; scanf("%d",&test); while(test--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { scanf("%d%d",&w[i],&t[i]); } solve(); while(m--) { scanf("%d%d",&u,&v); if(dp[v]&(1ULL<<u)) { printf("%d",pre[v][u]); tu=u; u-=w[pre[v][u]]; v-=t[pre[v][tu]]; while(u) { printf(" %d",pre[v][u]); tu=u; u-=w[pre[v][u]]; v-=t[pre[v][tu]]; } puts(""); } else printf("No solution!\n"); } } return 0; }