Cod sursa(job #768770)

Utilizator cvicentiuCiorbaru Vicentiu Marian cvicentiu Data 17 iulie 2012 17:50:19
Problema Oite Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <stdio.h>
#include <vector>
#include <list>
#define MAXOI 1025
#define MAXH 10007

struct grupare {
	int a, b;
	int sum;
};
using namespace std;

inline int contains(vector<vector<struct grupare> > &hash, int x, int a, int b) {
	vector<struct grupare>::iterator it;
	int result = 0;
	
	for (it = hash[x % MAXH].begin(); it != hash[x % MAXH].end(); it++) {
		if ((*it).sum == x && (*it).a != a && (*it).b != a &&
				(*it).a != b && (*it).b != b) {
			result ++;
		}
	}
	return result;
}
int main() {
	int C, L;
	int oi[MAXOI];
	
	int result = 0;
	vector<struct grupare> lst;
	vector<vector<struct grupare> > hash;

	FILE *f = fopen("oite.in", "r");
	FILE *g = fopen("oite.out", "w");

	for (int i = 0; i < MAXH; i++) {
		hash.push_back(lst);
	}
	fscanf(f, "%d%d", &C, &L);
	for (int i = 0; i < C; i++) {
		fscanf(f, "%d", &oi[i]);
	}

	struct grupare grupa;
	for (int i = 0; i < C; i++) {
		for (int j = i + 1; j < C; j++) {
			grupa.sum = oi[i] + oi[j];
			grupa.a = i;
			grupa.b = j;
			hash[grupa.sum % MAXH].push_back(grupa);
		}
	}

	for (int i = 0; i < C; i++) {
		for (int j = i + 1; j < C; j++) {
			int sum = oi[i] + oi[j];
			if (L - sum > 0) {
				result += contains(hash, L - sum, i, j);
			}
		}
	}
	fprintf(g, "%d", result / 6);

	fclose(f);
	fclose(g);


}