杭电acm1496
思路:首先,暴力会TLE,所以要把4个数分成2个和2个(关于这一点请阅读《挑战程序设计竞赛》)。注意到a*x1^2+b*x2^2的范围大小为2000000,我们不妨遍历前两个数,计算并记录在一个数组hash[]后,再遍历后两个数,并在hash[]中直接查找,这样复杂度就为O(n^2)(n=100)
完整代码:
- /*187ms,8044KB*/
- #include<cstdio>
- #include<cstring>
- const int maxn = 1000000;
- int hash[2 * maxn + 5];
- int main()
- {
- int a, b, c, d, i, j;
- long long ans;
- while (~scanf("%d%d%d%d", &a, &b, &c, &d))
- {
- if (a > 0 && b > 0 && c > 0 && d > 0 || a < 0 && b < 0 && c < 0 && d < 0)
- {
- puts("0");
- continue;
- }
- memset(hash, 0, sizeof(hash));
- for (i = 1; i <= 100; ++i)
- for (j = 1; j <= 100; ++j)
- ++hash[a * i * i + b * j * j + maxn];
- ans = 0L;
- for (i = 1; i <= 100; ++i)
- for (j = 1; j <= 100; ++j)
- ans += hash[-c * i * i - d * j * j + maxn];
- printf("%I64d\n", ans << 4);
- }
- }