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: ...
随机推荐
- Android各种模拟器使用笔记
[√]天天模拟器 优点: 缺点: 个人经验 ADB 版本过低的解决办法 去启动时的广告方法 去除多余进程方法 ADB无法连接到模拟器 原因分析: 解决方案: 安装APP(APK)时非常非常慢TTMNQ ...
- C# SqlBulkCopy类批量导入数据
特别注意 sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName); 插入的时候列的顺序可 ...
- oracle中过滤中文字符或者汉字的函数
CREATE OR REPLACE FUNCTION GET_CHINESE(P_NAME IN VARCHAR2) RETURN VARCHAR2 IS V_CODE VARCHAR2 ...
- TCP可靠传输详解
TCP提供了可靠的传输服务,这是通过下列方式提供的: 分块发送:应用数据被分割成TCP认为最适合发送的数据块.由TCP传递给IP的信息单位称为报文段或段(segment) 定时确认重传:当TCP发出一 ...
- 使用ffmpeg下载m3u8流媒体
安装 编译好的windows可用版本的下载地址(官网中可以连接到这个网站,和官方网站保持同步): http://ffmpeg.zeranoe.com/builds/ 或者: 百度网盘https://p ...
- jqury 如何获取 kindeditor 中textarea 的值
获取文本内容,可是的创建时怎么也不能获取,利用FF的firebug查看到自己所写的内容在一个iframe中,于是想从iframe中获取文本,想要用 $(“ifame”).html();获取内容,可是依 ...
- iOS 企业版 安装失败 原因
首先要吐槽下国内的论坛水分略多,以下问题大多是查询stackoverflow等论坛解决的.推荐一款软件,Log Guru,用来查看app安装时的系统日志,很多问题要看日志才知道错误点. 1.首先有几 ...
- PHPstorm如何安装vue.js插件
1.什么是PHPstorm? PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查.----来自百度百科 一句话:P ...
- 百度地图API简介
百度地图API简介 在此申明不是我写的,用的是别人的,仅限自己学习 百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松的访问百度服务和数据,构建功能 ...
- PLSQL使用技巧----加快你的编程效率
使用PLSQL 编程效率明显有所提高了 1.登录后默认自动选中My Objects 默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的 ...