HDU2141【hash】

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

分析:

用10^4打表  用10^2枚举

我的lower_bound一直挂  只能手写二分  不过还好

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 const int maxn = 505;
 8 int a[maxn], b[maxn], c[maxn], d[maxn * maxn];
 9
10 int cnt;
11 bool check(int num) {
12     int low = 0; int high = cnt - 1;
13     while(low <= high) {
14         int mid = ( low + high) >> 1;
15         if(d[mid] >= num) {
16             high = mid - 1;
17         } else {
18             low = mid + 1;
19         }
20     }
21     if(d[high + 1] == num) return true;
22     return false;
23 }
24
25 int main() {
26     int l, n, m;
27     int t = 1;
28     while(EOF != scanf("%d %d %d",&l, &n, &m) ) {
29         for(int i = 0; i < l; i++) scanf("%d",&a[i]);
30         for(int i = 0; i < n; i++) scanf("%d",&b[i]);
31         for(int i = 0; i < m; i++) scanf("%d",&c[i]);
32         cnt = 0;
33         for(int i = 0; i < l; i++) {
34             for(int j = 0; j < n; j++) {
35                 d[cnt++] = a[i] + b[j];
36             }
37         }
38         sort(d, d + cnt);
39         int s;
40         int num;
41         printf("Case %d:\n", t++);
42         scanf("%d",&s);
43         while(s--) {
44             scanf("%d",&num);
45             bool flag = false;
46             for(int i = 0; i < m; i++) {
47                 if(check(num - c[i]) ) {
48                     flag = true;
49                     break;
50                 }
51             }
52             if(flag) puts("YES");
53             else puts("NO");
54         }
55     }
56 }

时间: 2024-08-29 12:08:49

HDU2141【hash】的相关文章

POJ2299 Ultra-QuickSort 【树状数组】+【hash】

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39529   Accepted: 14250 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

【hash】BZOJ3751-[NOIP2014]解方程

[题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少orz 然而正解只有60+行 [前铺]f(n) mod p=f(n mod p) mod p 取四个素数,分别对每个ai取模.先预处理x=0..p-1的情况,直接代入多项式计算即可.再在O(m)时间内检验1..m,对于≥p的利用前铺公式可得.如果模四个素数结果均能得到0,说明这个数是方程的解. P.

【hash】Seek the Name, Seek the Fame

[哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame.

hdu 5183 Negative and Positive (NP)(STL-集合【HASH】)

题意: When given an array (a0,a1,a2,?an−1) and an integer K, you are expected to judge whether there is a pair (i,j)(0≤i≤j<n) which makes that NP−sum(i,j) equals to K true. Here NP−sum(i,j)=ai−ai+1+ai+2+?+(−1)j−iaj 1≤n≤1000000,−1000000000≤ai≤1000000000

[HDOJ 5183] Negative and Positive (NP) 【Hash】

题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCoder比赛的时候我写了 STL map, 然后TLE... 注意: Hash负数的时候 % 了一个质数,得到的是负数还要 + Mod !! 代码 #include <iostream> #include <cstdio> #include <cstdlib> #includ

BZOJ3555 [Ctsc2014]企鹅QQ 【hash】

题目 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体验,满足用户对社交.资讯.娱乐.交易等多方面的需求. 小Q是PenguinQQ网站的管理员,他最近在进行一项有趣的研究--哪些账户是同一个人注册的.经过长时间的分析,小Q发现同一个人注册的账户名称总是很相似的,例如Penguin1,Penguin2,Penguin3--于是小Q决定先对这种相似的情形进行

bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列【hash】

我%&&--&()&%????? 双模hashWA,unsigned long longAC,而且必须判断hash出来的数不能为0???? 我可能学了假的hash 这个题求个前缀和,然后目标是找到距离当前位置最远,且能使这两个数组差分后2-k位相同 hash把差分后数组的2到k位压起来即可,用map存这个hash值最早出现的位置 但是我还是不明白为啥hash值不能为0啊?? #include<iostream> #include<cstdio> #i

【HASH】【UVA 10125】 Sumset

传送门 Description 给定一个整数集合S,求一个最大的d,满足a+b+c=d,其中a,b,c,d∈S Input 多组数据,每组数据包括: 第一行一个整数n,代表元素个数 下面n行每行一个整数,代表集合元素 输入结束的标志为n=0. Output 对于每组数据,输出: 一行,如果有解,输出一个整数,代表最大的d:否则输出no solution Sample Input 5 2 3 5 7 12 5 2 16 64 256 1024 0 Sample Output 12 no solut

poj3974 Palindrome【回文】【Hash】

Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 13157   Accepted: 5028 Description Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you pr