杭电acm1496

思路:首先,暴力会TLE,所以要把4个数分成2个和2个(关于这一点请阅读《挑战程序设计竞赛》)。注意到a*x1^2+b*x2^2的范围大小为2000000,我们不妨遍历前两个数,计算并记录在一个数组hash[]后,再遍历后两个数,并在hash[]中直接查找,这样复杂度就为O(n^2)(n=100)


完整代码:

  1. /*187ms,8044KB*/  
  2.   
  3. #include<cstdio>  
  4. #include<cstring>  
  5. const int maxn = 1000000;  
  6.   
  7. int hash[2 * maxn + 5];  
  8.   
  9. int main()  
  10. {  
  11.     int a, b, c, d, i, j;  
  12.     long long ans;  
  13.     while (~scanf("%d%d%d%d", &a, &b, &c, &d))  
  14.     {  
  15.         if (a > 0 && b > 0 && c > 0 && d > 0 || a < 0 && b < 0 && c < 0 && d < 0)  
  16.         {  
  17.             puts("0");  
  18.             continue;  
  19.         }  
  20.         memset(hash, 0, sizeof(hash));  
  21.         for (i = 1; i <= 100; ++i)  
  22.             for (j = 1; j <= 100; ++j)  
  23.                 ++hash[a * i * i + b * j * j + maxn];  
  24.         ans = 0L;  
  25.         for (i = 1; i <= 100; ++i)  
  26.             for (j = 1; j <= 100; ++j)  
  27.                 ans += hash[-c * i * i - d * j * j + maxn];  
  28.         printf("%I64d\n", ans << 4);  
  29.     }  
  30. }  
posted @ 2015-12-20 16:21  StevenLuke  阅读(138)  评论(0编辑  收藏  举报