poj1840——Eqs
来源:互联网 发布:战列舰 知乎 编辑:程序博客网 时间:2024/06/03 21:21
题目大意: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 += 2500000
代码:转载自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;
}
- poj1840——Eqs
- POJ1840——Eqs
- poj1840——Eqs
- poj1840——Eqs(哈希)
- poj1840--Eqs
- poj1840 Eqs
- poj1840 Eqs
- Eqs poj1840
- POJ1840-Eqs
- POJ1840:Eqs
- poj1840 - Eqs
- poj1840 Eqs
- POJ1840-Eqs
- poj1840 Eqs
- POJ1840--------Eqs
- [POJ1840]-Eqs
- poj1840 Eqs
- 【POJ1840】Eqs
- HDU 5984Pocky
- Microsoft Word 2010
- Linux实验二
- fresco简单使用
- ubuntu 16.04 LTS
- poj1840——Eqs
- JQuery Validate配合Ajax完成校验
- 封装Retrofit
- git学习笔记
- 食物链(并查集)
- Microsoft Office Visio 2010
- 归并排序的实现
- Java中方法不能重载的三种情况
- 工具类总结(6)-订单生成工具类