Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
Sample Input
3 3 3 1 2 3 1 2 3 1 2 3 3 1 4 10
Sample Output
Case 1: NO YES NO
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
long long x,v;
int l,n,m,s;
long long L[555],N[555],M[555],LN[500*500+500];
bool sreach(long long k){
long long li=1;
long long r=v;
while(li<=r){
long long mid=(li+r)/2;
if(LN[mid]==k)return 1;
else if(LN[mid]<k)
li=mid+1;
else
r=mid-1;
}
return 0;
}
int main()
{
int t=0;
while(scanf("%d%d%d",&l,&n,&m)!=EOF){
for(int i=1;i<=l;i++)
scanf("%lld",&L[i]);
for(int i=1;i<=n;i++)
scanf("%lld",&N[i]);
for(int i=1;i<=m;i++)
scanf("%lld",&M[i]);
v=0;
for(int i=1;i<=l;i++){
for(int j=1;j<=n;j++){
LN[++v]=L[i]+N[j];
//cout << LN[v] << endl;
}
}
scanf("%d",&s);
printf("Case %d:\n",++t);
sort(LN+1,LN+v+1);
while(s--){
scanf("%lld",&x);
int flag=0;
for(int i=1;i<=m;i++){
if(sreach(x-M[i])){
flag=1;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
//cout << "Hello world!" << endl;
return 0;
}