POJ 1840

来源:互联网 发布:win10软件消失 编辑:程序博客网 时间:2024/06/05 15:28
#include<iostream>using namespace std;//**************常量定义*********************//const __int64 MAX=18750000;char Hash[2*MAX+1];void SetUpHash(int coe[6]){int x1,x2;__int64 key;for(x1=-50;x1<=50;x1++){  if(0==x1)  continue;for(x2=-50;x2<=50;x2++){  if(0==x2)continue;key=coe[1]*x1*x1*x1+coe[2]*x2*x2*x2+MAX;Hash[key]++;}}}int main(){   int a[6];    memset(Hash,0,sizeof(Hash));for(int i=1;i<6;i++)scanf("%d",&a[i]);SetUpHash(a);int x3,x4,x5;__int64 temp=0;__int64 count=0;for(x3=-50;x3<=50;x3++){   if(0==x3) continue;for(x4=-50;x4<=50;x4++){if(0==x4)continue;for(x5=-50;x5<=50;x5++){ if(0==x5)continue;temp=MAX+(a[3]*x3*x3*x3+a[4]*x4*x4*x4+a[5]*x5*x5*x5);count+=Hash[temp];}}}printf("%d",count);}



注意事项:

1.char Hash[2*MAX+1];Hash表定义为char类型,可以节约内存,char占用内存是short的一半,short是int的一半。

2.__int64 count=0;

   count+=Hash[temp];

   char类型会自动转化为int类型

3.

解题思路:

将方程移下位:a1x13+ a2x2= -(a3x33+ a4x43+ a5x53),当方程左边等于右边时,求得方程的一个解。

-(a3x33+ a4x43+ a5x53)项的范围是 [-3*50^4,3*50^4],所以MAX=3*50^4=18750000,即Hash表的长度必须定为2*MAX+1.

不然,如果MAX定义过小,MAX+(a[3]*x3*x3*x3+a[4]*x4*x4*x4+a[5]*x5*x5*x5)会小于0.



#include<iostream>using namespace std;//**************常量定义*********************//const __int64 MAX=12500000;short Hash[2*MAX+1];void SetUpHash(int coe[6]){int x1,x2;__int64 key;for(x1=-50;x1<=50;x1++){  if(0==x1)  continue;for(x2=-50;x2<=50;x2++){  if(0==x2)continue;key=coe[1]*x1*x1*x1+coe[2]*x2*x2*x2+MAX;Hash[key]++;}}}int main(){   int a[6];    memset(Hash,0,sizeof(Hash));for(int i=1;i<6;i++)scanf("%d",&a[i]);SetUpHash(a);int x3,x4,x5;__int64 temp=0;__int64 count=0;for(x3=-50;x3<=50;x3++){   if(0==x3) continue;for(x4=-50;x4<=50;x4++){if(0==x4)continue;for(x5=-50;x5<=50;x5++){ if(0==x5)continue;temp=MAX+(a[3]*x3*x3*x3+a[4]*x4*x4*x4+a[5]*x5*x5*x5);if((temp>=0)&&(temp<=2*MAX)) count+=Hash[temp];}}}printf("%d",count);}

因为:a1x13+ a2x23 的范围是-2*50^4~2*50^4.

所以coe[1]*x1*x1*x1+coe[2]*x2*x2*x2+MAX的范围是 0~4*50^4.

MAX+(a[3]*x3*x3*x3+a[4]*x4*x4*x4+a[5]*x5*x5*x5);的范围并不是0~4*50^4,所以,必须限制

if((temp>=0)&&(temp<=2*MAX)) count+=Hash[temp];否则,

temp=MAX+(a[3]*x3*x3*x3+a[4]*x4*x4*x4+a[5]*x5*x5*x5);可能小于0

哈希表,采用拉链法来解决冲突

#include<iostream>using namespace std;//******************常量定义**************//__int64 Myabs(__int64 x){if(x<0) return -x;elsereturn x;}const int M=3999;struct Node{   __int64 sum;//前两项和     short times;//重复的次数 Node* next;};Node* Hash[M+1];void SetUpHash(int a[6]){__int64 sum=0;int key;memset(Hash,0,sizeof(Hash));//初始化for(int x1=-50;x1<=50;x1++){if(0==x1)continue;for(int x2=-50;x2<=50;x2++){if(0==x2)continue;sum=-(a[1]*x1*x1*x1+a[2]*x2*x2*x2);key=Myabs(sum)%M;if(NULL==Hash[key]){Node* temp=new Node;temp->sum=sum;temp->times=1;temp->next=NULL;Hash[key]=temp;}else{Node* q=Hash[key];Node* p=NULL;bool flag=false;while(q){if(q->sum==sum){q->times++;flag=true;break;}else{p=q;q=q->next;}}if(!flag){Node* tmp=new Node;tmp->next=NULL;tmp->sum=sum;tmp->times=1;p->next=tmp;}}}}}int main(){   //初始化for(int i=0;i<M+1;i++)Hash[i]=NULL;int a[6];//输入系数for(int i=1;i<=5;i++)   scanf("%d",&a[i]);SetUpHash(a);int x3,x4,x5;__int64 sum;int key,count=0;for(x3=-50;x3<=50;x3++){if(0==x3)continue;for(x4=-50;x4<=50;x4++){if(0==x4)  continue;for(x5=-50;x5<=50;x5++){if(0==x5) continue; sum=a[3]*x3*x3*x3+a[4]*x4*x4*x4+a[5]*x5*x5*x5; key=Myabs(sum)%M; if(NULL==Hash[key]) continue; else { Node* current=Hash[key]; while(current) { if(current->sum==sum) { count+=current->times;break; } current=current->next; } }}}}printf("%d",count);}



0 0
原创粉丝点击