您的位置:首页 > 其它

POJ 1840 hash

2013-01-31 22:56 281 查看
//11225868	c00h00g	1840	Accepted	24868K	407MS	G++	1083B	2013-01-31 22:52:46
//我自己想的是移过去一项,即a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3=-a5*x5^3,然后将左边即为x,枚举左边,可以解出x5,然后判断x5是否在范围内
//不过,可能是精度的问题,结果都不正确
//然后看晚上用的都是hash,将算式分成两部分 a1*x1^3+a2*x2^3+a3*x3^3=-(a4*x4^3+a5*x5^3)一开始用左边做hash,但是超内存了,于是用右边
//做hash
//还有一点这里的hash不是存在和不存在,而是需要累加的,值相同,但对应的参数是不同的
//这一题有一个bug, while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)不写==5的时候会一直超时 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

const int base=50*50*50*50*2;
char hash[2*base+5];

int main(){
    int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5;
    while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)==5){
        memset(hash,0,sizeof(hash));                                    
        int res=0;
        for(int x1=-50;x1<=50;x1++)
            if(x1==0) continue;
            else for(int x2=-50;x2<=50;x2++)
                   if(x2==0) continue;
                   else  hash[a1*x1*x1*x1+a2*x2*x2*x2+base]+=1;

        for(int x3=-50;x3<=50;x3++)
            if(x3==0) continue;
            else for(int x4=-50;x4<=50;x4++)
                if(x4==0) continue;
                else for(int x5=-50;x5<=50;x5++)
                      if(x5==0) continue;
                      else{
                          int x=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
                          x*=-1;
                          if(x>=-base&&x<=base&&hash[x+base]) res+=hash[x+base];
                      }
        printf("%d\n",res);
    } 
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: