Cod sursa(job #768786)

Utilizator cvicentiuCiorbaru Vicentiu Marian cvicentiu Data 17 iulie 2012 18:12:46
Problema Oite Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#define MAXOI 1025
#define MAXH 99371

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

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<int> hash[MAXH];

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

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

	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) {
				int poz = (L - sum) % MAXH;
				for (int i = 0; i < (int) hash[poz].size(); i++) {
					if (hash[poz][i] == L - sum) {
						result ++;
					}
				}
			}
		}
		for (int j = 0; j < i; j++) {
			hash[oi[i] + oi[j] % MAXH].push_back(oi[i] + oi[j]);
		}
	}
	
	fprintf(g, "%d", result);

	fclose(f);
	fclose(g);


}