poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 13955 | Accepted: 6851 |
Description
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
Output
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 】的更多相关文章
- POJ 1840 Eqs 解方程式, 水题 难度:0
题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...
- POJ 1840 Eqs 二分+map/hash
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- POJ 1840:Eqs 哈希求解五元方程
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14169 Accepted: 6972 Description ...
- POJ 1840 Eqs
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 15010 Accepted: 7366 Description ...
- poj 1840 Eqs (hash)
题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...
- POJ 1840 Eqs(hash)
题意 输入a1,a2,a3,a4,a5 求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立 a,x取值在-50到50之间 直接暴力的话肯定会超时的 100的五次方 10e了都 ...
- POJ 1840 Eqs 暴力
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The ...
- POJ 1840 Eqs(乱搞)题解
思路:这题好像以前有类似的讲过,我们把等式移一下,变成 -(a1*x1^3 + a2*x2^3)== a3*x3^3 + a4*x4^3 + a5*x5^3,那么我们只要先预处理求出左边的答案,然后再 ...
- 【ZZ】详解哈希表的查找
详解哈希表的查找 https://mp.weixin.qq.com/s/j2j9gS62L-mmOH4p89OTKQ 详解哈希表的查找 2018-03-01 算法与数据结构 来自:静默虚空 http: ...
随机推荐
- Sql视图创建语句及修改视图
create view [dbo].[AllUsers] as select u.UserId, u.Firstname, u.Lastname, u.ts, am.Email, au.UserNam ...
- Weka学习之关联规则分析
步骤: (一) 选择数据源 (二)选择要分析的字段 (三)选择需要的关联规则算法 (四)点击start运行 (五) 分析结果 算法选择: Apriori算法参数含义 1.car:如果设为真,则会挖掘类 ...
- HDFS源码分析之FSImage文件内容(一)总体格式
FSImage文件是HDFS中名字节点NameNode上文件/目录元数据在特定某一时刻的持久化存储文件.它的作用不言而喻,在HA出现之前,NameNode因为各种原因宕机后,若要恢复或在其他机器上重启 ...
- 如何理解OOP?
OOP (Object Oriented Programming)面向对象编程 1.它符合我们现在思考习惯 2.它让一些复杂的事情变得更加简单 3.它让操作者比那成了指挥者
- 【Python + Mysql】之用pymysql库连接Mysql数据库并进行增删改查操作
用pip下载pymysql并引用 具体请参考文章: <Python之MySQL数据库增删改查操作> <python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删 ...
- cxf 创建动态webService
D:\developTools\apache-cxf-2.5.2\samples\wsdl_first_dynamic_client CXF 方法 cxf方法 serviceInfo.getBindi ...
- mysql 存储过程初探
使用存储过程好处在于: 1.隐藏敏感的算法,避免被正常的开发人员看到,把业务逻辑隐藏在数据库中,而非程序代码里 2.简化应用代码程序,放到数据库里肯定就对程序代码简化有好处了 3.不同的开发语言都可以 ...
- 由浅到深理解ROS(2)
ROS文件系统 用户可以直接参看官网:http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念:Packa ...
- AngularJS form $addControl 注冊控件control
需求背景: 在form中使用编写的某component directive时.想通过form's name来对form中控件进行操作, 如使用$invalid等来ng-disabled btn. 解决 ...
- ASIHTTPRequest中文入门教程全集 http://www.zpluz.com/thread-3284-1-1.html
本文转载至 目录 3 第 1 章 创建和运行请求 5 1.1. 创建一个同步请求 5 1.2. 创建一个异步请求 5 1.3. 使用程序块(blocks ) 6 1.4. 使用 ...