POJ 1840 hash

//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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值