您的位置:首页 > 其它

POJ 1840 Eqs 二分+map/hash

2015-07-17 23:21 543 查看
Description

Consider equations having the following form:
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

The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.
Output

The output will contain on the first line the number of the solutions for the given equation.
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=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
//*******************************************************************
__int64 a[1000005];
map< int ,int > mp;
int t;
int jug(__int64 x)
{

int l=0;
int r=t;
int xx;
int mid;
while(l<=r)
{
mid=(l+r)/2;
if(a[mid]>x)
{
r=mid-1;
}
else if(a[mid]<x)
{
l=mid+1;
if(a[l]==x)return mp[x];
}
else return mp[x];
}
return 0;
}
int main()
{

int a1,a2,a3,a4,a5;
t=0;
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
for(int x1=-50; x1<=50; x1++)
{
if(x1==0) continue;
for(int x2=-50; x2<=50; x2++)
{
if(x2==0)continue;
a[++t]=(a1*x1*x1*x1+a2*x2*x2*x2);
if(mp.count(a[t]))
mp[a[t]]++;
else mp[a[t]]=1;
}
}
sort(a+1,a+t+1);
int ans=0;
for(int x3=-50; x3<=50; x3++)
{
if(x3==0)continue;
for(int x4=-50; x4<=50; x4++)
{
if(x4==0) continue;
for(int x5=-50; x5<=50; x5++)
{
if(x5==0) continue;
__int64 aaa=-1*(a3*x3*x3*x3+x4*a4*x4*x4+a5*x5*x5*x5);
ans+=jug(aaa);
}
}
}
printf("%d\n",ans);
return 0;
}


这是哈希标记法


#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=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
//**********************************************************

short  hash[25000001];
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,0,sizeof(hash));
for(x1=-50; x1<=50; x1++)
{
if(x1==0)
continue;
for(x2=-50; x2<=50; x2++)
{
if(x2==0)
continue;
sum=(a1*x1*x1*x1+a2*x2*x2*x2)*-1;
if(sum<0)sum+=maxn;
hash[sum]++;
}
}
int cnt = 0;
for(x3=-50; x3<=50; x3++)
{
if(x3==0)
continue;
for(x4=-50; x4<=50; x4++)
{
if(x4==0)
continue;
for(x5=-50; x5<=50; x5++)
{
if(x5==0)
continue;
sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
if(sum<0)sum+=maxn;
cnt+=hash[sum];
}
}
}
printf("%d\n",cnt);
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: