Skip to content

Instantly share code, notes, and snippets.

@oilover
Created September 10, 2014 05:14
Show Gist options
  • Save oilover/6c97bafebf8415976d37 to your computer and use it in GitHub Desktop.
Save oilover/6c97bafebf8415976d37 to your computer and use it in GitHub Desktop.
zoj3812.cpp
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#include<cmath>
#include<vector>
typedef long long ll;
#define prt(k) cout<< #k" = " << k<< " \n"; ///
typedef unsigned long long ull;
const int N = 402;
const int M = 200000;
const int MM = 50;
ull valid[M+20];
int aa[N], bb[N];
int first[M+10][MM+1];
int main()
{
int re; scanf("%d", &re);
while(re--)
{
int n, q;
scanf("%d%d", &n, &q);
memset(valid, 0, sizeof valid);
memset(first, -1, sizeof first);
valid[0] = 1;
for(int i = 0; i < n; i++)
{
int a, b;
cin>>b>>a;
aa[i]= a; bb[i] = b;
for(int j = M; j >= a; j--)
{
ull tmp = valid[j];
valid[j] |= ((valid[j - a] << b) & ((1ull << MM + 1) - 1)) ;
for(ull bit = tmp ^ valid[j]; bit; bit = bit - 1 & bit)
{
first[j][__builtin_ctzll(bit)] = i; /// bit = 1<<n
}
}
}
while( q-- )
{
int b, a;
cin>> b>> a;
if(valid[a]>>b & 1)
{
bool isfirst = true;
while(a && b)
{
int i = first[a][b];
if(isfirst) isfirst = 0;
else putchar(' ');
cout<<i+1;
a -= aa[i];
b -= bb[i];
}
cout<<endl;
}
else cout<<"No solution!\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment