SPOJ4580 ABCDEF(meet in the middle)
题意
Sol
发现abcdef是互不相关的
那么meet in the middle一下。先算出abc的,再算def的
注意d = 0的时候不合法(害我wa了两发。。)
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 101, SS = 2e6 + 10;
map<LL, LL> mp;
int N;
LL a[MAXN], ans;
int a1[SS], c1, a2[SS], c2, cnt[SS];
int main() {
// ios::sync_with_stdio(false);
cin >> N;
for(int i = 1; i <= N; i++) cin >> a[i];
sort(a + 1, a + N + 1);
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
for(int k = 1; k <= N; k++)
a1[++c1] = a[i] * a[j] + a[k];
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
for(int k = 1; k <= N; k++)
if(a[i] != 0) a2[++c2] = a[i] * (a[j] + a[k]);
sort(a1 + 1, a1 + c1 + 1);
sort(a2 + 1, a2 + c2 + 1);
for(int i = 1, j = 1; i <= c2; i++) {
if(i != 1 && (a2[i] == a2[i - 1])) {cnt[i] = cnt[i - 1]; continue;}
while(a1[j] <= a2[i] && j <= c1) {
if(a1[j] == a2[i]) cnt[i]++;
j++;
}
}
for(int i = 1; i <= c2; i++) ans += cnt[i];
cout << ans;
return 0;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。