POJ 1840 Eqs 二分+map/hash
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 题意:
给出5个数(<=50)a1,a2,a3,a4,a5 ,分别与5个未知数的3次方 联立方程=0 为a1x1^3+ a2x2^3+a3x3^3+ a4x4^3+ a5x5^3=0 |xi|<=50并xi!=0 求有多少组解。
题解
二分+map标记,先暴力出x1,x2,x3对应的a1x13+ a2x23+ a3x33 ; 存入数组中,再对应暴力 去 二分查找出等于 负的a4*x43次方+a5*x53次方 相应的下标 及对应个数; 代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define MOD 1000000007
#define maxn 20000001
using namespace std;
typedef long long LL;
int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//*******************************************************************
__int64 a[];
map< int ,int > mp;
int t;
int jug(__int64 x)
{ int l=;
int r=t;
int xx;
int mid;
while(l<=r)
{
mid=(l+r)/;
if(a[mid]>x)
{
r=mid-;
}
else if(a[mid]<x)
{
l=mid+;
if(a[l]==x)return mp[x];
}
else return mp[x];
}
return ;
}
int main()
{ int a1,a2,a3,a4,a5;
t=;
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
for(int x1=-; x1<=; x1++)
{
if(x1==) continue;
for(int x2=-; x2<=; x2++)
{
if(x2==)continue;
a[++t]=(a1*x1*x1*x1+a2*x2*x2*x2);
if(mp.count(a[t]))
mp[a[t]]++;
else mp[a[t]]=;
}
}
sort(a+,a+t+);
int ans=;
for(int x3=-; x3<=; x3++)
{
if(x3==)continue;
for(int x4=-; x4<=; x4++)
{
if(x4==) continue;
for(int x5=-; x5<=; x5++)
{
if(x5==) continue;
__int64 aaa=-*(a3*x3*x3*x3+x4*a4*x4*x4+a5*x5*x5*x5);
ans+=jug(aaa);
}
}
}
printf("%d\n",ans);
return ;
}
这是哈希标记法
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define maxn 25000000
#define inf 1000000007
using namespace std;
typedef long long LL;
int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//********************************************************** short hash[];
int main()
{
int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,sum;
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
memset(hash,,sizeof(hash));
for(x1=-; x1<=; x1++)
{
if(x1==)
continue;
for(x2=-; x2<=; x2++)
{
if(x2==)
continue;
sum=(a1*x1*x1*x1+a2*x2*x2*x2)*-;
if(sum<)sum+=maxn;
hash[sum]++;
}
}
int cnt = ;
for(x3=-; x3<=; x3++)
{
if(x3==)
continue;
for(x4=-; x4<=; x4++)
{
if(x4==)
continue;
for(x5=-; x5<=; x5++)
{
if(x5==)
continue;
sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
if(sum<)sum+=maxn;
cnt+=hash[sum];
}
}
}
printf("%d\n",cnt);
return ;
}
POJ 1840 Eqs 二分+map/hash的更多相关文章
- poj 1840 Eqs (hash)
题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...
- poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 6851 Description ...
- POJ 1840 Eqs(hash)
题意 输入a1,a2,a3,a4,a5 求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立 a,x取值在-50到50之间 直接暴力的话肯定会超时的 100的五次方 10e了都 ...
- 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
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 15010 Accepted: 7366 Description ...
- POJ 1840 Eqs(乱搞)题解
思路:这题好像以前有类似的讲过,我们把等式移一下,变成 -(a1*x1^3 + a2*x2^3)== a3*x3^3 + a4*x4^3 + a5*x5^3,那么我们只要先预处理求出左边的答案,然后再 ...
- POJ 1840 Eqs 暴力
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- poj 2318 叉积+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13262 Accepted: 6412 Description ...
随机推荐
- 关于QQ使用的一些代码
http://wiki.open.qq.com/wiki/website/网站接入wiki索引
- struts2框架 初始别
struts2 是webwork和struts合并而来. 1.下载struts2 说明: Full Distribution: 为完整版下载,建议下载它 Example Applications:st ...
- wxPython中文教程入门实例
这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下 wxPython中文教程入门实例 wx.Window 是一个基类 ...
- Unity3D 给模型偏移纹理
给模型偏移纹理 using UnityEngine; using System.Collections; [RequireComponent(typeof(Renderer))] public cla ...
- Qt 改变图片大小
void Setting_TabProduct::changeImageSize(int width,int height,QString imgFile) { QPixmap pixmap(imgF ...
- 整数划分问题-解法汇总(暂有DP-递归)
整数划分问题是一个锻炼组合数学,递归以及动态规划很好的例子,虽然问题看似简单,但是其中玄机万千,有人转化成为背包问题,有人用生成函数解,有人以此作为企业面试题目,可见这种问题的认可度还是很高的. 整数 ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [第三方]AFNetWorking3.0网络框架使用方法
官网地址https://github.com/AFNetworking/AFNetworking #import <AFNetworking.h> - (void)viewDidLoad ...
- MyEclipse8.5可用注册码(到2018年)
转载自:http://blog.csdn.net/z123252520/article/details/45873159 Subscriber:zy Subscriber Code:mLR8ZC-85 ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...