POJ 1840 Eqs (hash)

题目:http://poj.org/problem?id=1840 http://acm.nyist.net/JudgeOnline/problem.php?pid=136

题意:给定a1,a2,a3,a4,a5,求方程a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0解的个数,未知数和系数的范围在[-50,50],其中任何一个未知数不能为0。

分析:将a1x13+ a2x23的值存入hash表,在hash表里面查询-(a3x33+ a4x43+ a5x53的个数。poj上面过了,不知为何在nyoj上过不了,有大牛过了请指点一下。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
#define MOD 300007
#define MAXN 1000017

struct node
{
	int value;
	int next;
};

struct Hash
{
	int Table[MOD],cnt;
	node List[MAXN];
	void my_clear()
	{
		cnt=0; 
		for(int i=0;i<MOD;i++)
			Table[i]=-1;
	}
	void my_insert(int x)
	{
		int hash;
		if(x<0)
			hash=(-x+1)%MOD;
		else
			hash=x%MOD;
		List[cnt].next=Table[hash];
		List[cnt].value=x;
		Table[hash]=cnt;
		cnt++;
	}
	int my_query(int x)
	{
		int hash,ans=0;
		if(x<0)
			hash=(-x+1)%MOD;
		else
			hash=x%MOD;
		hash=Table[hash];
		while(hash!=-1)
		{
			if(List[hash].value==x)
				ans++;
			hash=List[hash].next;
		}
		return ans;
	}
}H;

int main()
{	
	int i,j,k,s[200];
	for(i=-50,j=0;i<=50;i++,j++)
		s[j]=i*i*i;
	int a1,a2,a3,a4,a5,ncase,ans;
	while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)!=EOF)
	{
		H.my_clear();
		for(i=0;i<=100;i++)
		{
			if(i==50) continue;
			for(j=0;j<=100;j++)
			{
				if(j==50)	continue;
				H.my_insert(a1*s[i]+a2*s[j]);
			}
		}
		ans=0;
		for(i=0;i<=100;i++)
		{
			if(i==50) continue;
			for(j=0;j<=100;j++)
			{
				if(j==50) continue;
				for(k=0;k<=100;k++)
				{
					if(k==50) continue;
					ans+=H.my_query(-(a3*s[i]+a4*s[j]+a5*s[k]));
				}	
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


终于过了,POJ上面居然没有0 0 0 0 0 这组数据,让我水过了-  -,南阳上面有这组数据。去重就好了。

 
#include <iostream>
#include <cstdio>

using namespace std;
typedef long long LL;

#define MOD 300007
#define MAXN 111111

struct node
{
	LL value;
	LL count;
	LL next;
};

struct Hash
{
	LL Table[MOD];
	node List[MAXN];
	int cnt;
	
	void Clear()
	{
		cnt=0;
		for(int i=0;i<MOD;i++)
			Table[i]=-1;
	}
	void Insert(LL x)
	{
		LL hash,t;
		if(x<0)
			hash=(-x+666)%MOD;
		else
			hash=(x+7)%MOD;
		t=Table[hash];
		while(t!=-1)
		{
			if(List[t].value==x)
			{
				List[t].count++;
				return ;
			}
			t=List[t].next;
		}
		List[cnt].value=x;
		List[cnt].count=1;
		List[cnt].next=Table[hash];
		Table[hash]=cnt;
		cnt++;
	}
	LL Query(LL x)
	{
		LL hash;
		if(x<0)
			hash=(-x+666)%MOD;
		else
			hash=(x+7)%MOD;
		hash=Table[hash];
		while(hash!=-1)
		{
			if(List[hash].value==x)
				return List[hash].count;
			hash=List[hash].next;
		}
		return 0; 
	}
}H;

int main()
{	
	LL i,j,k,s[200];
	for(i=-50,j=0;i<=50;i++,j++)
		s[j]=i*i*i;
	LL a1,a2,a3,a4,a5,ncase,ans;
	scanf("%lld",&ncase);
	while(ncase--)
	{
		scanf("%lld%lld%lld%lld%lld",&a1,&a2,&a3,&a4,&a5);
		H.Clear();
		for(i=0;i<=100;i++)
		{
			if(i==50) continue;
			for(j=0;j<=100;j++)
			{
				if(j==50)	continue;
				H.Insert(a1*s[i]+a2*s[j]);
			}
		}
		ans=0;
		for(i=0;i<=100;i++)
		{
			if(i==50) continue;
			for(j=0;j<=100;j++)
			{
				if(j==50) continue;
				for(k=0;k<=100;k++)
				{
					if(k==50) continue;
					ans+=H.Query(-(a3*s[i]+a4*s[j]+a5*s[k]));
				}	
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}                



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值