Eqs
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 13955   Accepted: 6851

Description

Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=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
分析:将等式方程左边3、4项移到等号右边,是方程变成一个另外一种等式。
枚举左边打表,枚举右边查表。 或者 枚举右边打表,枚举左边查表。
复杂度:n^3+n^2(logn) 或者 n^2+n^3(logn) 复杂度两者差不多。
代码1:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define N 100000+100 using namespace std; int f[1000100]; //复杂度=三层枚举打表+两层枚举*二分
//(记得数组要在二层打表的基础上扩大一下 否则访问越界) int B_search(int low, int high, int key)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(f[mid]==key ){
int u=mid-1, v=mid+1;
while(f[u]==key) u--;
while(f[v]==key) v++;
return v-u-1;
}
else if(f[mid]>key){
high=mid-1;
}
else low=mid+1;
}
return 0;
} int main()
{
int a, b, c, d, e;
int i, j, k;
while(scanf("%d %d %d %d %d", &a, &b, &c, &d, &e)!=EOF)
{
int cnt=0;
for(i=-50; i<=50; i++){
if(i!=0)
for(j=-50; j<=50; j++){
if(j!=0)
for(k=-50; k<=50; k++){
if(k!=0){
int cur=-c*i*i*i-d*j*j*j-e*k*k*k;
f[cnt++]=cur;
}
}
}
} sort(f, f+cnt); int ans=0;
for(i=-50; i<=50; i++){
if(i!=0){
for(j=-50; j<=50; j++){
if(j!=0){
int cur=a*i*i*i + b*j*j*j;
ans=ans+B_search(0, cnt-1, cur);
}
}
}
}
printf("%d\n", ans );
}
return 0;
}

代码2:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define N 100000+100 using namespace std; int f[20000]; //复杂度=两层枚举打表+三层枚举*二分
int B_search(int low, int high, int key)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(f[mid]==key ){
int u=mid-1, v=mid+1;
while(f[u]==key) u--;
while(f[v]==key) v++;
return v-u-1;
}
else if(f[mid]>key){
high=mid-1;
}
else low=mid+1;
}
return 0;
} int main()
{
int a, b, c, d, e;
int i, j, k;
while(scanf("%d %d %d %d %d", &a, &b, &c, &d, &e)!=EOF)
{
queue<int>q; while(!q.empty()) q.pop(); //-d*i*i*i-e*j*j*j 全部入队列
int cnt=0; for(i=-50; i<=50; i++){
if(i!=0)
for(j=-50; j<=50; j++){
if(j!=0){
int cur=-d*i*i*i-e*j*j*j;
f[cnt++]=cur; }
}
}
sort(f, f+cnt);
//printf("************\n"); int ans=0; for(i=-50; i<=50; i++){
if(i!=0)
for(j=-50; j<=50; j++){
if(j!=0)
for(k=-50; k<=50; k++){
if(k!=0){
int cur=a*i*i*i + b*j*j*j + c*k*k*k;
ans=ans+B_search(0, cnt-1, cur);
}
}
}
}
printf("%d\n", ans );
}
return 0;
}
												

poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】的更多相关文章

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

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

  2. POJ 1840 Eqs 二分+map/hash

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  3. POJ 1840:Eqs 哈希求解五元方程

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14169   Accepted: 6972 Description ...

  4. POJ 1840 Eqs

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15010   Accepted: 7366 Description ...

  5. poj 1840 Eqs (hash)

    题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...

  6. POJ 1840 Eqs(hash)

    题意  输入a1,a2,a3,a4,a5  求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立   a,x取值在-50到50之间 直接暴力的话肯定会超时的   100的五次方  10e了都 ...

  7. POJ 1840 Eqs 暴力

      Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The ...

  8. POJ 1840 Eqs(乱搞)题解

    思路:这题好像以前有类似的讲过,我们把等式移一下,变成 -(a1*x1^3 + a2*x2^3)== a3*x3^3 + a4*x4^3 + a5*x5^3,那么我们只要先预处理求出左边的答案,然后再 ...

  9. 【ZZ】详解哈希表的查找

    详解哈希表的查找 https://mp.weixin.qq.com/s/j2j9gS62L-mmOH4p89OTKQ 详解哈希表的查找 2018-03-01 算法与数据结构 来自:静默虚空 http: ...

随机推荐

  1. GoogleMap的鼠标点击标注、搜索和设置城市的简单应用

    资源 Google Map API包含了大量的文档.示例和各种资料.在使用前需要申请自己的密钥 墙内要用:http://maps.google.cn/maps/api/js? 墙外可用:https:/ ...

  2. Swagger跨域访问

    我们用springboot开发完后,需要前端vue用swagger跨域,默认是不能跨域的,所以需要我们后台设置跨域访问,将下面代码完整复制即可. 在springboot项目中新建class : Cor ...

  3. URL检测脚本

    #!/bin/bash# filename : 8_5_1.sh function usage(){ echo "usage:$0 url" exit 1} function ch ...

  4. C#中YieldReturn的用法

    Yield Return 和 Yield Break 的出现是为了简化迭代器. 类如果能被遍历则必须有IEnumerator<string> GetEnumerator() 方法, 并有用 ...

  5. java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException

    问题描述:启动tomcat服务器的时候,报找不到JoranException类的异常 原因:tomcat中没有logback-core-1.1.2.jar包 解决方法:在tomcat中的lib目录添加 ...

  6. Ubuntu 16.04下编译安装Apache2.4和PHP7结合

    Ubuntu 16.04下编译安装Apache2.4和PHP7结合,并安装PDOmysql扩展. 1.编译安装apache2.4.20 1 第一步: ./configure --prefix=/usr ...

  7. 为什么Goroutine能有上百万个,Java线程却只能有上千个?

      作者|Russell Cohen   译者|张卫滨   本文通过 Java 和 Golang 在底层原理上的差异,分析了 Java 为什么只能创建数千个线程,而 Golang 可以有数百万的 Go ...

  8. ubuntu 16.04安装visual studio code 提示libnss3版本低:NSS >= 3.26 is required

    Linux Ubuntu 1604安装VS CODE之后,执行./code报错误: [3781:0914/160851.489979:FATAL:nss_util.cc(632)] NSS_Versi ...

  9. Dijkstra 算法——计算有权最短路径(边有权值)

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在理解 Dijkstra 的思想并用源代码加以实现: 0.2)最短路径算法的基础知识,参见 http://blog. ...

  10. 如何使CSS--better(系列二)

    上一篇文章(如何使CSS--beter 系列一)中  分析了一下 什么样子的代码是高效的 应该避免什么样子的代码, 那么什么样子的代码是更容易扩展的? 什么代码是更好维护的? 什么代码是更好的? 下边 ...