Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

hash....为何就wa了呢??

Posted by ning at 2006-03-28 19:20:50 on Problem 2785
//pku2785 4 Values whose Sum is 0 hash+o(n2)
// hash
#include<stdio.h>
#include<memory.h>
//#include<conio.h>
int A[4005],B[4005],C[4005],D[4005];
const int HSize=4000*4005*3;
int HTable[4005*4000*3];
int Visited[4005*4000*3];
int Hash(int x)
{
	int pos=x*10257-3;
	if(pos<0)
		pos*=-1;
	pos%=HSize;
	return pos; 
}
void ToHash(int x)
{
	int pos,ii=1;
	for(pos=Hash(x);Visited[pos];pos+=ii++*3-1,pos%=HSize)
		;
	HTable[pos]=x;
	Visited[pos]=1;
}

int Find(int x) 
{
	int pos,ii=1;
	for(pos=Hash(x);Visited[pos];pos+=ii++*3-1,pos%=HSize)
		if(HTable[pos]==x)
			return 1;
	return 0;
}

int main()
{
	int n,rst=0;
	int ii,jj;
	for(;scanf("%d",&n)!=EOF;)
	{
	memset(Visited,0,sizeof(Visited));
	for(ii=0;ii<n;ii++)
	{
		scanf("%d%d%d%d",A+ii,B+ii,C+ii,D+ii);
	}
	for(ii=0;ii<n;ii++)
		for(jj=0;jj<n;jj++)
			ToHash(A[ii]+B[jj]);
	for(ii=0,rst=0;ii<n;ii++)
		for(jj=0;jj<n;jj++)
			if( Find(-1*(C[ii]+D[jj])) )
				rst++;
	printf("%d",rst);
	}
//	getch();
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator