poj-1840-Eqs
来源:互联网 发布:淘宝超低价宝贝集锦 编辑:程序博客网 时间:2024/06/14 08:55
Description
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.
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
给出一个5元3次方程,输入其5个系数,求它的解的个数
其中系数 ai∈[-50,50] 自变量xi∈[-50,0)∪(0,50]
一开始想着暴力水过去,因为前几天蓝桥杯好像有一道差不多题是暴力水过去的,但是发现这个O(n5)完全水不过去啊。所以后来网上搜了题解,发现别人比你就是聪明,想的方法就是不一样些,佩服那些大神啊
接下来:hash
我们先定义一个映射数组hash[],初始化为0
对于方程左边,当x1=m , x2= n时得到sum,则把用hash[]记录sum : hash[sum]++,表示sum这个值出现了1次
之所以是记录“次数”,而不是记录“是否已出现”,
因为不能保证函数的映射为 1对1 映射,更多的是存在 多对1映射。
例如当 a1=a2时,x1=m , x2= n我们得到了sum,但x1=n , x2= m时我们也会得到sum,但是我们说这两个是不同的解,这就是 多对1 的情况了,如果单纯记录sum是否出现过,则会使得 解的个数 减少。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;short hash[25000001];int main(){ int a1,a2,a3,a4,a5; while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)!=EOF) { memset(hash,0,sizeof(hash)); for(int x1=-50;x1<=50;x1++) { if(!x1) continue; for(int x2=-50;x2<=50;x2++) { if(!x2) continue; int sum=(a1*x1*x1*x1 + a2*x2*x2*x2)*(-1); if(sum<0) sum+=25000000; hash[sum]++; } } int so=0; for(int x3=-50;x3<=50;x3++) { if(!x3) continue; for(int x4=-50;x4<=50;x4++) { if(!x4) continue; for(int x5=-50;x5<=50;x5++) { if(!x5) continue; int sum=a3*x3*x3*x3 + a4*x4*x4*x4 + a5*x5*x5*x5; if(sum<0) sum+=25000000; if(hash[sum]) so+=hash[sum]; } } } printf("%d\n",so); } return 0;}
为了使得 搜索sum是否出现 的操作为o(1),我们把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
0 0
- poj 1840 Eqs
- POJ 1840 Eqs
- poj 1840 Eqs
- POJ 1840 Eqs
- POJ 1840 Eqs hash
- POJ-1840 Eqs【Hash】
- poj 1840 Eqs 【hash】
- POJ 1840 Eqs
- poj-1840 Eqs
- POJ 1840 Eqs
- POJ 1840 Eqs
- poj 1840 Eqs
- POJ 1840 Eqs
- POJ 1840 Eqs
- POJ 1840(Eqs)
- POJ 1840 Eqs
- POJ 1840 Eqs
- POJ 1840 Eqs
- PHP学习(十四)--数组的相关处理函数
- 深入理解java虚拟机(五):hotspot垃圾收集算法实现
- 当年折在此~~
- 你真的了解IE浏览器么?
- 深入理解java虚拟机(六):java垃圾收集分析实战(内存分配与回收策略)
- poj-1840-Eqs
- 深入理解java虚拟机(七):java垃圾收集分析总结
- Day3eve
- [Java]构造方法私有化-->单例模式
- 用好你的业余时间
- LeetCode(34)-Palindrome Number
- spring(10)------Bean三种管理方式
- Java 中的 hashcode一
- 服务器创建用户并允许用户远程登录服务器