1. 程式人生 > >SPOJ4580 ABCDEF(meet in the middle)

SPOJ4580 ABCDEF(meet in the middle)

() i++ bits out syn main const c++ turn

題意

題目鏈接

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;
}

SPOJ4580 ABCDEF(meet in the middle)