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 ACM_henry at 2008-10-15 16:50:08 on Problem 2785

#include <iostream>
using namespace std;
int a[4010],b[4010],c[4010],d[4010];
const int prim=10^7;
class node
{
public:
	int key;
	int num;
	node *next;
	node()
	{
		key=0;
		num=0;
		next=NULL;
	}
}hash[10^7];
void insert(int sum)
{
	int aim;
	if(sum<0)
	{
		aim=(-sum)%prim;
	}
	else aim=sum%prim;
	node *p=&hash[aim];
	if(p->num==0)
	{
		p->key=sum;
		p->next=NULL;
		p->num=1;
		return;
	}
	while(1)
	{
		if(p->key==sum)
		{
			p->num++;
			return ;
		}
		if(p->next==NULL)
		{
			p=p->next;
			break;
		}
		else p=p->next;
	}
	p=new node;
	p->next=NULL;
	p->num=1;
	p->key=sum;
	return;
}
int find(int sum)
{
	int aim;
	if(sum<0)
	{
		aim=(-sum)%prim;
	}
	else aim=sum%prim;
	node *p=&hash[aim];
	while(1)
	{
		if(p->key==sum)
		{
			return p->num;
		}
		if(p->next==NULL)
		{
			return 0;
		}
		else p=p->next;
		
	}
	return 0;
}
int main()
{
	int sum,ans;
	ans=0;
	int n;
	cin>>n;
	int i,j;
	for(i=0;i<n;i++)
	{
		scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			sum=a[i]+b[j];
			insert(sum);
		}
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{			
			sum=c[i]+d[j];
			sum=-sum;
			ans+=find(sum);
		}
	}
	//printf("%I64d\n",ans);
	cout<<ans<<endl;
	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