POJ 1840 Eqs 二分+map/hash

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次方 相应的下标  及对应个数;

代码:

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <queue>
10 #include <map>
11 #include <stack>
12 #define MOD 1000000007
13 #define maxn 20000001
14 using namespace std;
15 typedef long long LL;
16 int read()
17 {
18     int x=0,f=1;
19     char ch=getchar();
20     while(ch<‘0‘||ch>‘9‘)
21     {
22         if(ch==‘-‘)f=-1;
23         ch=getchar();
24     }
25     while(ch>=‘0‘&&ch<=‘9‘)
26     {
27         x=x*10+ch-‘0‘;
28         ch=getchar();
29     }
30     return x*f;
31 }
32 //*******************************************************************
33 __int64 a[1000005];
34 map< int ,int > mp;
35 int t;
36 int jug(__int64 x)
37 {
38
39     int l=0;
40     int r=t;
41     int xx;
42     int mid;
43     while(l<=r)
44     {
45         mid=(l+r)/2;
46         if(a[mid]>x)
47         {
48             r=mid-1;
49         }
50         else if(a[mid]<x)
51         {
52             l=mid+1;
53             if(a[l]==x)return mp[x];
54         }
55         else return mp[x];
56     }
57   return 0;
58 }
59 int main()
60 {
61
62     int a1,a2,a3,a4,a5;
63     t=0;
64     scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
65     for(int x1=-50; x1<=50; x1++)
66     {
67         if(x1==0) continue;
68         for(int x2=-50; x2<=50; x2++)
69         {
70             if(x2==0)continue;
71             a[++t]=(a1*x1*x1*x1+a2*x2*x2*x2);
72             if(mp.count(a[t]))
73             mp[a[t]]++;
74             else mp[a[t]]=1;
75         }
76     }
77     sort(a+1,a+t+1);
78     int ans=0;
79     for(int x3=-50; x3<=50; x3++)
80     {
81         if(x3==0)continue;
82         for(int x4=-50; x4<=50; x4++)
83         {
84             if(x4==0) continue;
85             for(int x5=-50; x5<=50; x5++)
86             {
87                 if(x5==0) continue;
88                 __int64 aaa=-1*(a3*x3*x3*x3+x4*a4*x4*x4+a5*x5*x5*x5);
89                 ans+=jug(aaa);
90             }
91         }
92     }
93     printf("%d\n",ans);
94     return 0;
95 }
  这是哈希标记法
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <queue>
10 #include <map>
11 #include <stack>
12 #define maxn 25000000
13 #define inf 1000000007
14 using namespace std;
15 typedef long long LL;
16 int read()
17 {
18     int x=0,f=1;
19     char ch=getchar();
20     while(ch<‘0‘||ch>‘9‘)
21     {
22         if(ch==‘-‘)f=-1;
23         ch=getchar();
24     }
25     while(ch>=‘0‘&&ch<=‘9‘)
26     {
27         x=x*10+ch-‘0‘;
28         ch=getchar();
29     }
30     return x*f;
31 }
32 //**********************************************************
33
34 short  hash[25000001];
35 int main()
36 {
37     int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,sum;
38     scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
39     memset(hash,0,sizeof(hash));
40     for(x1=-50; x1<=50; x1++)
41     {
42         if(x1==0)
43             continue;
44         for(x2=-50; x2<=50; x2++)
45         {
46             if(x2==0)
47                 continue;
48             sum=(a1*x1*x1*x1+a2*x2*x2*x2)*-1;
49             if(sum<0)sum+=maxn;
50                 hash[sum]++;
51         }
52     }
53     int cnt = 0;
54     for(x3=-50; x3<=50; x3++)
55     {
56         if(x3==0)
57             continue;
58         for(x4=-50; x4<=50; x4++)
59         {
60             if(x4==0)
61                 continue;
62             for(x5=-50; x5<=50; x5++)
63             {
64                 if(x5==0)
65                     continue;
66                 sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
67                 if(sum<0)sum+=maxn;
68                     cnt+=hash[sum];
69             }
70         }
71     }
72     printf("%d\n",cnt);
73     return 0;
74 }
				
时间: 2024-12-13 14:33:30

POJ 1840 Eqs 二分+map/hash的相关文章

POJ 1840 Eqs(hash)

题意  输入a1,a2,a3,a4,a5  求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立   a,x取值在-50到50之间 直接暴力的话肯定会超时的   100的五次方  10e了都    然后可以考虑将等式变一下形   把a1*x1^3+a2*x2^3移到右边   也就是-(a1*x1^3+a2^x2^3)=a3*x3^3+a4*x4^3+a5*x5^3 考虑到a1*x1^3+a2^x2^3的最大值50*50^3+50*50^3=12500000  这个数并不大  可以开这么大

POJ 1840 Eqs(哈希)

题目地址:POJ 1840 sad...整个比赛期间一直以为是用什么定理或数学公式推导来做..一直没仔细看..结果最后5分钟的时候才看到每个元素的数据范围只是[-50,50]...算了..就算看到了也做不出来..因为会MLE,解决MLE需要把hash数组的定义类型定义成short...这我是不可能想出来的....也没遗憾了.. 这题就是先求前两个for循环,将结果用hash数组存起来.再进行后面三个for循环,如果值为相反数的话,说明和为0.然后记录有多少个. 代码如下: #include <i

POJ 1840 Eqs(暴力)

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,

POJ 1840.Eqs

Eqs Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1840 Description Consider equations having the following form: a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=0 The coefficients are given integers fr

poj 1840 Eqs (Hash)

/* 这题10^8的暴力可以出答案 但是 慢..... 有个小小的bug 出题人卡int 却没注意short 用short可以不用hash 直接搞 49428K 313MS */ #include<iostream> #include<cstdio> #include<cstring> #define base 12500000 using namespace std; int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,ans; short f[125

POJ 1840 Eqs Hash + 中途相遇法

把等式分成两拨算用中途相遇法就好了. 不过要注意的是这里不能用map,会超时,要自己手写hash,我重载了[]操作符之后用起来和map差不多,很随意 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <qu

poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】

Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 6851 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It i

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

poj 1840 简单hash

http://poj.org/problem?id=1840 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