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

求解五元三次方程。

暴力破解

枚举所有的可能。

直接暴力需要的时间非常巨大 1005=1010

而可以将一部分移向到另一侧有a1x13+a2x23=-(a3x33+a4x43+a5x53)

这样所需的时间就变成了1002+1003少了许多数量级

先枚举等式左侧的情况,在枚举等式右侧的情况。

使用一个数组来存储左侧得出一个数值的步骤数,如果等式右侧能得出这个数,则此为可行解

由于牵扯的数据巨大,因此数组可以开成unsigned char来保存

AC代码:GitHub

 1 /*
 2 By:OhYee
 3 Github:OhYee
 4 HomePage:http://www.oyohyee.com
 5 Email:[email protected]`e.com
 6 Blog:http://www.cnblogs.com/ohyee/
 7
 8 かしこいかわいい?
 9 エリーチカ!
10 要写出来Хорошо的代码哦~
11 */
12
13 #include <cstdio>
14 #include <algorithm>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <iostream>
19 #include <vector>
20 #include <list>
21 #include <queue>
22 #include <stack>
23 #include <map>
24 using namespace std;
25
26 //DEBUG MODE
27 #define debug 0
28
29 //循环
30 #define REP(n) for(int o=0;o<n;o++)
31
32 const int maxn = 6250001*5;
33 unsigned char cnt[maxn*2];
34
35 bool Do() {
36     int a[5];
37     REP(5)
38         if(scanf("%d",&a[o]) == EOF)
39             return false;
40
41     memset(cnt,0,sizeof(cnt));
42
43     //map<long long,int> m;
44     for(int i = -50;i <= 50;i++)
45         for(int j = -50;j <= 50;j++)
46             if(i != 0 && j != 0) {
47                 long long temp = maxn + a[0] * i*i*i + a[1] * j*j*j;
48                 cnt[temp]++;
49             }
50
51     int ans = 0;
52     for(int i = -50;i <= 50;i++)
53         for(int j = -50;j <= 50;j++)
54             for(int k = -50;k <= 50;k++)
55                 if(i != 0 && j != 0 && k != 0) {
56                     long long temp = maxn -(a[2] * i*i*i + a[3] * j*j*j + a[4] * k*k*k);
57                     ans += cnt[temp];
58                 }
59
60     printf("%d\n",ans);
61     return true;
62 }
63
64 int main() {
65     while(Do());
66     return 0;
67 }
时间: 2024-10-24 13:30:55

POJ 1840.Eqs的相关文章

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

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

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 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 二分+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,

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 简单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

POJ 1804 Eqs

C - Eqs Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-5