poj1840——Eqs

题目大意:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,( ai∈[-50,50]  xi∈[-50,0)∪(0,50])给出五个系数,求出解的个数

输入:a1 a2 a3 a4 a5

输出:解的个数

分析:暴力枚举肯定超时,所以对方程做一个变形:

           

           即先枚举x1、x2,记录打表,再枚举x3、x4、x5,若左右相等就得到了一个解

           哈希数组hash[sum]记录的是等号左侧的值sum出现的次数(hash[]初始化为0),由于sum可能为负数,所以我们对sum的上界进行翻倍,当得到sum<0时,sum += 25000000

代码:转载自https://www.cnblogs.com/shenben/p/5619582.html

#include<cstdio>
#include<cstring>
using namespace std;
#define N 25000000
#define t 50
int a1,a2,a3,a4,a5;
short hash[N+1];
int main(){
    while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)==5){
        memset(hash,0,sizeof hash);
        for(int x1=-t;x1<=t;x1++){
            if(!x1) continue;
            for(int x2=-t;x2<=t;x2++){
                if(!x2) continue;
                int sum=(a1*x1*x1*x1+a2*x2*x2*x2)*(-1);
                if(sum<0) sum+=N;
                hash[sum]++;
            }
        }
        int ans=0;
        for(int x3=-t;x3<=t;x3++){
            if(!x3) continue;
            for(int x4=-t;x4<=t;x4++){
                if(!x4) continue;
                for(int x5=-t;x5<=t;x5++){
                    if(!x5) continue;
                    int sum=(a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5);
                    if(sum<0) sum+=N;
                    if(hash[sum])
                    ans+=hash[sum];
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值