POJ 2785 4 Values whose Sum is 0

用hash写了一遍,hash的空间复杂度不是盖的

/*Accepted 176460K 3907MS C++ 1006B 2012-05-25 09:13:41 */

#include<cstdio>

#include<cstring>

#include<cstdlib>

#define cal(x) ( (x + (1 << 29 | 1) * M) % MAX)

#define LL __int64

const int MAX = 20000001;

const int M = 17531753;

const int MAXN = 4005;

int hash[MAX], cnt[MAX];

bool vis[MAX];

int a[MAXN],b[MAXN], c[MAXN], d[MAXN];

void inhash( int x)

{

    LL t = x;

    t = cal(t);

    while( vis[t] && hash[t] != x)

        t = cal(t);

    if( !vis[t] ) {vis[t] = true; cnt[t] = 1; hash[t] = x;}

    else cnt[t] ++;

}



int find( int x)

{

    LL t = x;

    t = cal(t);

    while( vis[t] && hash[t] != x)

        t = cal(t);

    if( vis[t]) return cnt[t];

    return 0;

}



int main()

{

    int n;

    while( scanf( "%d", &n) == 1)

    {

        for( int i = 0; i < n; i ++)

            scanf( "%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);

        for( int i = 0; i < n; i ++)

            for( int j = 0; j < n; j ++)

                inhash( a[i] + b[j]);

        int ans = 0;

        for( int i = 0; i < n; i ++)

            for( int j = 0; j < n; j ++)

                ans += find( -(c[i] + d[j] ));

        printf( "%d\n", ans);

    }

    return 0;

}

 

你可能感兴趣的:(value)