题目描述
Consider equations having the following form:
a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.

Determine how many solutions satisfy the given equation.

Input
The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.

Output
The output will contain on the first line the number of the solutions for the given equation.

Sample Input
37 29 41 43 47

Sample Output
654

大致题意: 给你一个等式和取值范围,问有多少种不同的取值方式使得等式成立

思路:先将方程变化成-(a1*x1^3+a2*x2^3) = a3*x3^3+a4*x4^3+a5*x5^3,如此一来缩短了时间复杂度,否则直接五个嵌套for循环必然会超时的
其次,我们把sum作为下标,那么hash数组的上界就取决于a1 a2 x1 x2的组合,四个量的极端值均为50

因此上界为 50*50^3+50*50^3=12500000,由于sum也可能为负数,因此我们对hash[]的上界进行扩展,扩展到25000000,当sum<0时,我们令sum+=25000000存储到hash[],负数就改为正数表示了

由于数组很大,必须使用全局定义

同时由于数组很大,用int定义必然会MLE,因此要用char或者short定义数组,推荐short

代码如下

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<map>using namespace std;short hash[25000001];
int main()
{int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,sum;while(cin>>a1>>a2>>a3>>a4>>a5){memset(hash,0,sizeof(hash));for(x1=-50;x1<=50;x1++){if(!x1)continue;for(x2=-50;x2<=50;x2++){if(!x2)continue;sum=-1*(a1*x1*x1*x1+a2*x2*x2*x2);if(sum<0)sum+=25000000;hash[sum]++;}}int cnt=0;for(x3=-50;x3<=50;x3++){if(!x3)continue;for(x4=-50;x4<=50;x4++){if(!x4)continue;for(x5=-50;x5<=50;x5++){if(!x5)continue;sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;if(sum<0)sum+=25000000;  cnt+=hash[sum];}}}cout<<cnt<<endl;}return 0;
}

Eqs POJ - 1840相关推荐

  1. poj 1840 Eqs

    题目连接 http://poj.org/problem?id=1840 Eqs Description Consider equations having the following form:  a ...

  2. POJ 1840 Eqs 解方程式, 水题 难度:0

    题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...

  3. 【POJ No. 1840】公式 Eqs

    [POJ No. 1840]公式 Eqs POJ题目地址 [题意] 考虑方程a1 x1^3 +a2 x2^3 +a3 x3^3=0,系数是在[-50,50]区间的整数,xi ∈[-50,50],xi ...

  4. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

  5. POJ 超详细分类

    POJ 各题算法 1000    A+B Problem            送分题     49%    2005-5-7 1001    Exponentiation         高精度   ...

  6. (精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目

    POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: ...

  7. POJ刷题列表——正在进行

    转载自:http://blog.csdn.net/pyub153/article/details/50480333 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心 ...

  8. POJ的题目分类(两个版本)

    版本一: 简单题 1000A+B Problem 1001Exponentiation 1003 Hangover 1004 Financial Management 1005 I Think I N ...

  9. poj题目详细分类及算法推荐题目

    DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题  ...

最新文章

  1. Matlab中画图以及plot函数及legend函数详解
  2. VTK:vtkCursor2D用法实战
  3. iOS ARC环境下dealloc的使用
  4. CodeSmith 4.0 正式版发布
  5. 动作基类 CCAction
  6. linux软链接上传svn,svn 软链接问题
  7. 有生之年必看!千古第一奇书《山海经》到底是怎样的一本书?
  8. 写给中学生的算法入门:学代码之前看这篇就够了
  9. jQuery:无限循环两个或者多个事件 click / toggle between two functions
  10. git 无法 push 远程仓库 【Note about fast-forwards】
  11. linux tar 命令 详解
  12. 网路防火墙iptables
  13. 特征提取与检测(五) : LBP特征
  14. daily scrum 10.31
  15. CS224N笔记——深入GRU和LSTM
  16. 吉林大学高等数学A3作业答案新版
  17. 【国外网站软件下载慢解决】
  18. s3c6410 开发板Linux系统支持 K9GAG08U0E的方法(第二篇)
  19. kafka消费者报错:Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
  20. python自动玩游戏_python学习:(2)自动玩2048游戏

热门文章

  1. WIN10系统从睡眠状态唤醒后电脑变卡顿
  2. 在Ubuntu下安装vim
  3. 好用的便签APP排行榜前十名?
  4. easyui主界面布局easyui-layout用法一例
  5. 每日一例,练就编程高手
  6. 深度学习 VS 传统的机器学习
  7. 万豪 oracle,BRG币(BridgeOracle)价格行情怎么样?万豪BRG币走势最新消息
  8. Activity启动过程源码流程梳理和解读
  9. 产业巨头布局移动互联网 惠普比联想棋高一筹
  10. 电脑如何备份文件,怎么同步?