题意:从四个集合各选一个数,使和等于0,问有多少种选法。
分析:求出来所有ai + bi,在里面找所有等于ci + di的个数。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 40000 + 10; const int MAXT = 16000000 + 10; using namespace std; int a[MAXN], b[MAXN], c[MAXN], d[MAXN]; int sum[MAXT]; int main(){ int T; scanf("%d", &T); while(T--){ int n; scanf("%d", &n); for(int i = 0; i < n; ++i){ int aa, bb, cc, dd; scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]); } int cnt = 0; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ sum[cnt++] = a[i] + b[j]; } } sort(sum, sum + cnt); int ans = 0; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ int t = -(c[i] + d[j]); ans += upper_bound(sum, sum + cnt, t) - lower_bound(sum, sum + cnt, t);//sum中可能有许多与t相等的数,都要计算上 } } printf("%d\n", ans); if(T) printf("\n"); } return 0; }