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: ...
随机推荐
- 基于Ranking-CNN的年龄识别(CVPR_2017)
作为学习记录,将所做PPT摘录如下:
- Resharper快捷键及用法
建议你使用 Reshaper 的快捷键,不要担心 Reshaper 会把你原来的快捷键设置给覆盖了,因为如果某个快捷键和 VS 是冲突的,Reshaper会让你自己选择需要使用 VS 还是 Resha ...
- fedora20配置静态ip
在linux的世界里.给主机设置固定ip是这么做的(使用root用户): 1.查看要配的网络接口 用ifconfig查看查看在用的网卡接口,一般都用第一个如:eth0,en1,em1等 2.停用网络自 ...
- 不同手机根据坐标计算控件、图片的像素,px 与 dp, sp换算公式?
参考该帖子:http://www.cnblogs.com/bluestorm/p/3640786.html PPI = Pixels per inch,每英寸上的像素数,即 "像素密度&qu ...
- linux上复制行到另一个文件
1.复制3行到5行数据到ab.sh文件,不覆盖ab.sh文件中的已有内容 :3,5w>>ab.sh 2.复制3行到5行数据到ab.sh文件,覆盖ab.sh文件中的已有内容 :3,5w!ab ...
- "fcitx按ctrl+space没反应"解决方法
如果是KDM.GDM.LightDM,打开~/.xprofile.如果是startx.Slim,打开~/.xinitrc.(没有就新建一个) export GTK_IM_MODULE=fcitxexp ...
- yaffs2在am335x上实施
一.yaffs2文件烧写(通过网口) uboot下输入如下命令: setenv ipaddr 192.168.2.110;setenv serverip 192.168.2.222;saveenv n ...
- Python读取word文档(python-docx包)
最近想统计word文档中的一些信息,人工统计的话...三天三夜吧 python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.read ...
- 解决Mysql - can't get stat of (errcode:13)
[1]线上环境问题 大清早来上班,被运维扔了一个错误日志截图,如下: Mysql 导入数据失败! [2]分析问题 记得如此类似的问题,好像已经都解决了.为啥又复现了呢?难道是自己上次没有考虑全场景?或 ...
- 开源项目之easyrtmp
https://github.com/bigbluebutton86/EasyRTMP/tree/master/src http://dl.linux-sunxi.org/SDK/A20/A20_SD ...