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,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

题目

  

  芒果君:这道题是裸暴力,但是今天我们有一个新的思路,就是用哈希来优化暴力。我们把五项i=1->5 表示为Xi,那么很明显X1+X2+X3==-X4-X5,这样我们把等式左边存到哈希表,然后让等式右边去找左边,大大减小了枚举的时间复杂度。

  

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<map>
 5 #define mod 100003
 6 #define ll long long
 7 using namespace std;
 8 int hl[mod],a1,a2,a3,a4,a5,cnt,ans;
 9 int cal(int x){return x*x*x;}
10 struct H{
11     int val,ne;
12 }Hash[1100000];
13 void insert(int x)
14 {
15     int key=abs(x)%mod;
16     Hash[++cnt].val=x;
17     Hash[cnt].ne=hl[key];
18     hl[key]=cnt;
19 }
20 int search(int x)
21 {
22     int sum=0;
23     int key=abs(x)%mod;
24     for(int i=hl[key];i;i=Hash[i].ne)    if(Hash[i].val==x) sum++;
25     return sum;
26 }
27 int main()
28 {
29     scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
30     for(int i=-50;i<=50;++i)if(i)
31         for(int j=-50;j<=50;++j)if(j)
32             for(int k=-50;k<=50;++k)if(k)
33                 insert(a1*cal(i)+a2*cal(j)+a3*cal(k));
34     for(int i=-50;i<=50;++i)if(i)
35         for(int j=-50;j<=50;++j)if(j)
36             ans+=search(-a4*cal(i)-a5*cal(j));
37     printf("%d",ans);
38     return 0;
39 }
时间: 2024-08-16 06:54:12

POJ 1840:Eqs的相关文章

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(哈希)

题目地址: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(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 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o