Problem Description
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
Author
wangye
Source
HDU 2007-11 Programming Contest
#include <bits/stdc++.h> using namespace std; int l,n,m; int a[505],b[505],c[505]; int d[250050]; int t=0; void erfen(int X) { for(int i=0;i<m;i++) //枚举较小的数组 { int x=X-c[i]; int L=0,R=t-1; //搜索的数组范围 while(L<=R) { //二分搜索较大的数组 int mid=(L+R)/2; if(d[mid]<x) L=mid+1; else if(d[mid]>x) R=mid-1; else { printf("YES\n"); return; } } } printf("NO\n"); return; } int main() { int ans=0; while(~scanf("%d%d%d",&l,&n,&m)) { printf("Case %d:\n",++ans); for(int i=0;i<l;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[i]); for(int i=0;i<m;i++) scanf("%d",&c[i]); t=0; for(int i=0;i<l;i++) for(int j=0;j<n;j++) d[t++]=a[i]+b[j]; sort(d,d+t); //搜索前排序 int s; scanf("%d",&s); while(s--) { int x; scanf("%d",&x); erfen(x); } } return 0; }
#include <bits/stdc++.h> //set 内存超限 using namespace std; int l,n,m; int a[505],b[505],c[505]; int d[250050]; set<int>ss; int main() { int ans=0; while(~scanf("%d%d%d",&l,&n,&m)) { ss.clear(); printf("Case %d:\n",++ans); for(int i=0;i<l;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[i]); for(int i=0;i<m;i++) scanf("%d",&c[i]); for(int i=0;i<l;i++) for(int j=0;j<n;j++) ss.insert(a[i]+b[j]); int s; scanf("%d",&s); while(s--) { int x; scanf("%d",&x); int flag=0; for(int i=0;i<m;i++) { if(ss.count(x-c[i])) { flag=1; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod=177777; //玄学取模 int l,n,m; int a[505],b[505],c[505]; int head[190050],tol; struct node { int to,next; }rode[2500005]; bool find(int y) { int x=y%mod; for(int i=head[x];i!=-1;i=rode[i].next) if(rode[i].to==y)return 1; return 0; } void add(int y) { int x=y%mod; rode[tol].to=y; rode[tol].next=head[x]; head[x]=tol++; } int main() { int ans=0; while(~scanf("%d%d%d",&l,&n,&m)) { tol=0; memset(head,-1,sizeof(head)); printf("Case %d:\n",++ans); for(int i=0;i<l;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[i]); for(int i=0;i<m;i++) scanf("%d",&c[i]); for(int i=0;i<l;i++) for(int j=0;j<n;j++) if(!find((a[i]+b[j]))) add(a[i]+b[j]); int s; scanf("%d",&s); while(s--) { int x; scanf("%d",&x); int flag=0; for(int i=0;i<m;i++) { if(find(x-c[i])) { flag=1; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } } return 0; }