标签:style blog http color os io for 问题
问题重述:
给定系数a1,a2, ..,a5,求满足a1 * x1 ^ 3 + a2 * x2 ^ 3 +... + a5 * x5 ^ 3 = 0的 xi 的组数。其中ai, xi都在[-50, 50]内,且xi != 0。
算法:
1)用h[i]记录满足a1 * x1 ^3 + a2 * x2 ^ 3 = i 的x1, x2组数。
2)令ans = 0, 循环x1, x2, x3的值,若 tmp = a3 * x3^3 + a4 * x4^3 + a5 * x5^3, h[-tmp] != 0, 则 ans += h[-tmp]。
AC代码
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 const int maxn = 25000010; 8 const int offset = maxn / 2; 9 short h[maxn]; 10 int a[5]; 11 12 void _hash() 13 { 14 memset( h, 0, sizeof(h) ); 15 for (int i = -50; i <= 50; i++) { 16 if (i == 0) continue; 17 for (int j = -50; j <= 50; j++) { 18 if (j == 0) continue; 19 h[a[0] * i * i * i + a[1] * j * j * j + offset]++; 20 } 21 } 22 } 23 24 25 int main() 26 { 27 for (int i = 0; i < 5; i++) 28 cin >> a[i]; 29 _hash(); 30 int ans = 0; 31 for (int i = -50; i <= 50; i++) 32 for (int j = -50; j <= 50; j++) 33 for (int k = -50; k <= 50; k++) { 34 if (i * j * k != 0) { 35 int tmp = a[2] * i * i * i + 36 a[3] * j * j * j + 37 a[4] * k * k * k; 38 if (offset - tmp < maxn && offset - tmp >=0) 39 if ( h[offset - tmp] != 0) 40 ans += h[offset - tmp]; 41 } 42 } 43 cout << ans << endl; 44 return 0; 45 }
标签:style blog http color os io for 问题
原文地址:http://www.cnblogs.com/junxie/p/3873905.html