Cod sursa(job #1742265)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 16 august 2016 02:00:51
Problema Puteri Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

ifstream fin("puteri.in");
ofstream fout("puteri.out");

const int dim = 100005;
const int vmax = 128;
const int expmax = 64;

int n, a[dim], b[dim], c[dim];
int dp[expmax + 1][expmax + 1][expmax + 1];

long long Calc(int mod) {

	memset(dp, 0, sizeof dp);

	long long ret = 0;
	for (int i = 1; i <= n; ++i) {

		int x = mod - a[i] % mod, y = mod - b[i] % mod, z = mod - c[i] % mod;
		x = (x == mod ? 0 : x); y = (y == mod ? 0 : y); z = (z == mod ? 0 : z);
		
		if (x <= expmax && y <= expmax && z <= expmax)
			ret += dp[x][y][z];

		dp[a[i] % mod][b[i] % mod][c[i] % mod]++;

	}

	return ret;

}

int pinexCoef[vmax + 5];
bool sieve[vmax + 5];

long long Sieve(void) {

	long long ret = 0;

	memset(sieve, false, sizeof sieve);
	memset(pinexCoef, -1, sizeof pinexCoef);

	for (int i = 2; i <= vmax; ++i) {

		if (!sieve[i]) {

			for (int j = i; j <= vmax; j += i)
				pinexCoef[j] *= -1, sieve[j] = true;
			for (int j = i * i; j <= vmax; j += i * i)
				pinexCoef[j] = 0;

		}

		if (pinexCoef[i])
			ret += pinexCoef[i] * Calc(i);

	}

	return ret;

}

int main() {

	fin >> n;
	for (int i = 1; i <= n; ++i)
		fin >> a[i] >> b[i] >> c[i];

	fout << Sieve() << '\n';

	return 0;

}

//Trust me, I'm the Doctor!