POJ 1840(哈希)

题目链接:http://poj.org/problem?id=1840

看到这道题第一感觉是很熟悉,想起hdu上的一道题。

不过不同的是,这道题如果采用相同方法去做,数组要开始6kw,需要用short开,应该是数据不强吧。

另外,这道题map还可以过。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=62500010;
const int maxnHash=31250000;
int m,t,n;
int a1,a2,a3,a4,a5;
short hashTable[maxn];
int p[50];

int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
#endif
	while(~scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)){
		for(int i=-50;i<=50;i++){
			if(!i) continue;
			for(int j=-50;j<=50;j++){
				if(!j) continue;
				int tmp=a1*i*i*i+a2*j*j*j;
				hashTable[tmp+maxnHash]++;
			}
		}
		int ans=0;
		for(int i=-50;i<=50;i++){
			if(!i) continue;
			for(int j=-50;j<=50;j++){
				if(!j) continue;
				for(int k=-50;k<=50;k++){
					if(!k) continue;
					int tmp=a3*i*i*i+a4*j*j*j+a5*k*k*k;
					ans+=hashTable[-tmp+maxnHash];
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

附上hdu1496:

Consider equations having the following form: 


a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.


It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.


Determine how many solutions satisfy the given equation.
 


Input
The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.
 


Output
For each test case, output a single line containing the number of the solutions.
 


Sample Input
1 2 3 -4
1 1 1 1
 


Sample Output
39088
0

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=2000010;
int a,b,c,d;
int Hash[maxn];
int p[105];

void init(){
    for(int i=1;i<=100;i++)
        p[i]=i*i;
}

int main(){
    init();
    while(~scanf("%d%d%d%d",&a,&b,&c,&d)){
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0)){
            printf("0\n");
            continue;
        }
        memset(Hash,0,sizeof(Hash));
        for(int i=1;i<=100;i++){
            for(int j=1;j<=100;j++){
                Hash[(a*p[i]+b*p[j])+1000000]++;
            }
        }
        int sum=0;
        for(int i=1;i<=100;i++){
            for(int j=1;j<=100;j++){
                sum+=Hash[-(c*p[i]+d*p[j])+1000000];
            }
        }
        printf("%d\n",sum*16);
    }
    return 0;
}



你可能感兴趣的:(POJ 1840(哈希))