1152-4 values whose sum is 0 (easy-to-use hash tag, multi-cycle simplification)

Source: Internet
Author: User

I have to say that this question is cool!

Let's talk about the idea first:

1. Get the four-round loop, blow up, it will obviously time out.

2. to process multiple loops, enumerate A + B + C. You only need to find the opposite number of A + B + C in D.

3. enumerate a + B. You only need to find the opposite number of A + B in C + D. TMD times out!

4. The loop can only be optimized to this extent, and then the hash table is used for direct calling.


The hash table for this question is also new knowledge, because the value of A + B in this question may be very large, therefore, the previous hash table retrieval is not easy to use (because the array cannot be so big). Thanks to Gao Shen's blog, I learned a trick. Haha, the following content is carried:

Hash can be used to mark a number that cannot be marked with arrays.

For example, if we judge whether the number 100 exists after it, we can use the array vis [100] = 1; to indicate whether it has appeared.

However, for a large number of 10000000000, the array is powerless. At this time, it will be hashed.

 struct Hash_map{    static const int mask=0x7fffff;    int p[8388608],q[8388608];    void clear()    {        for(int i=0; i<=mask; ++i)            q[i]=0;    }    int& operator [](int k)    {        int i;        for(i=k&mask; q[i]&&p[i]!=k; i=(i+1)&mask);        p[i]=k;        return q[i];    }}hash;

This is a hash structure that reloads [] in the structure body. You can directly use Hash [10000000000] ++; to mark the success. You can use int x = hash [10000000000] directly during query. It is very convenient. Haha, that is to say, this hash directly marks a particularly large number.

# Include <stdio. h> # include <iostream> # include <map> # include <vector> using namespace STD; struct hash_map {static const int mask = 0x7fffff; int P [8388608], Q [8388608]; void clear () // Initialization is 0 {for (INT I = 0; I <= mask; ++ I) Q [I] = 0 ;} int & operator [] (int K) // reload "[]" {int I; for (I = K & mask; Q [I] & P [I]! = K; I = (I + 1) & Mask); P [I] = K; return Q [I] ;}} hash; int main () {int N, kase = 0; CIN> N; while (n --) {If (Kase) printf ("\ n"); vector <int> V1, V2, V3, V4, v5, V6; int M, Ji = 0; CIN> m; while (M --) // input data to the variable length Array {int temp; CIN> temp; v1.push _ back (temp); CIN> temp; v2.push _ back (temp); CIN> temp; v3.push _ back (temp); CIN> temp; v4.push _ back (temp);} int len1 = v1.size (), len2 = v2.size (), len3 = v3.size (), len4 = v4.size (); hash. clear (); For (INT I = 0; I <len1; I ++) for (Int J = 0; j <len2; j ++) hash [V1 [I] + V2 [J] ++; For (INT I = 0; I <len3; I ++) for (Int J = 0; j <len4; j ++) Ji + = hash [-V3 [I]-V4 [J]; // This is a pitfall, since each A + B is not necessarily only one type, it is possible that the values of A and B are different, but the values of A + B are the same and there are several groups, TMD printf ("% d \ n", JI); Kase = 1;} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.