思路:首先,暴力會TLE,是以要把4個數分成2個和2個(關于這一點請閱讀《挑戰程式設計競賽》)。注意到a*x1^2+b*x2^2的範圍大小為2000000,我們不妨周遊前兩個數,計算并記錄在一個數組hash[]後,再周遊後兩個數,并在hash[]中直接查找,這樣複雜度就為O(n^2)(n=100)
完整代碼:
[cpp] view
plaincopy
- /*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);
- }
- }