POJ 1840.Eqs
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 47Sample 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:oyohyee@oyohyee.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 }
然而,我并不能保证我说的是对的。请自行验证,如有错误,请指正
新博客地址
https://www.oyohyee.com
https://www.oyohyee.com