POJ 1840 Eqs (hash)
2015-03-11 09:51
357 查看
题目: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上过不了,有大牛过了请指点一下。
代码:
终于过了,POJ上面居然没有0 0 0 0 0 这组数据,让我水过了- -,南阳上面有这组数据。去重就好了。
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; }
相关文章推荐
- POJ 1840 Eqs [HASH]
- poj 1840 Eqs(Hash)
- poj 1840 Eqs (hash)
- POJ 1840 Eqs (Hash)
- POJ 1840 Eqs (大整数的Hash)
- hdu 题目1496 Equations , POJ 题目1840 Eqs (整数Hash)
- POJ1840: Eqs(hash问题)
- POJ 1840 Eqs 二分+map/hash
- POJ题目1840 Eqs(hash)
- poj-1840 Eqs(hash)
- POJ-1840 Eqs Hash
- poj 1840 Eqs , hash
- Poj 1840 Eqs(Hash)
- POJ 1840 Eqs(枚举+HASH)
- POJ 1840 Eqs(hash)
- POJ 1840 Eqs.cpp 【 Hash 】
- POJ 1840 Eqs hash
- POJ 1840 Eqs(hash)
- POJ 1840 Eqs(简单hash入门)
- poj 1840 Eqs (Hash)