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: ...
随机推荐
- Ansible 安装jdk
1. 在hosts文件添一个group,里面是你需要安装jdk的ip,如: [newhosts]192.168.2.155 ansible_ssh_user=hadoop ansible_ssh_pa ...
- 出租车Jt/T 905协议与部标1078协议融合的网约车视频监控平台
出租车jt/t 905协议,是jt/t 808协议的一个变种,设计者将部标808协议拿过来,并不是单纯的增加网约车相关的指令集,而且对原有的指令如定位0×0200指令也进行了修改,经过一通剧烈的修改, ...
- [译]NeHe教程 - 创建一个OpenGL窗体
原文: Setting Up An OpenGL Window 欢迎阅读我的OpenGL教程.我是一个热爱OpenGL的普通码农!我第一次听到OpenGL是在3Dfx刚发布他们给Voodoo I显卡的 ...
- 【Python+selenium Wendriver API】之下拉框定位
上代码: # coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains impo ...
- Mysql 复制表数据(表结构相同)
[1]Mysql 复制表数据(表结构相同) -- 方式一: create table table_name_dest as select * from table_name_src; -- 方式二: ...
- vim visual模式 复制
按ESC再按“V”,进入visual模式 用键盘向左向右箭头选中要复制的文字,按两下"Y"键 再到要粘贴的地方,按“P”键即可. 转自: http://jingyan.baidu. ...
- 录音整理文字工具otranscribe简介
网址: http://otranscribe.com/ 首先载入音频文件,支持 mp3, ogg, webm, wav (HTML5 无需将文件上传至服务器,可保护隐私),然后就可以边听边整理了. 通 ...
- web安全之SQL注入---第五章 如何预防SQL注入 ?
5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a-zA-Z]{6,}$/5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a ...
- LNMP环境搭建(二:MySQL)
1.获取MySQL官方的rpm包,根据操作系统与需要安装的MySQL版本进行选择,官方地址:https://www.mysql.com/downloads/ # cd /usr/local/src # ...
- 【BZOJ3488】[ONTAK2010]Highways 扫描线+树状数组
[BZOJ3488][ONTAK2010]Highways Description 给一棵n个点的树以及m条额外的双向边q次询问,统计满足以下条件的u到v的路径:恰经过一条额外的边不经过树上u到v的路 ...