Cod sursa(job #1742266)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 16 august 2016 02:02:52
Problema Puteri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 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];
int modulo[vmax + 5];

long long Calc(int mod) {

	memset(dp, 0, sizeof dp);
	for (int i = 0; i <= vmax; ++i)
		modulo[i] = i % mod;

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

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

		dp[modulo[a[i]]][modulo[b[i]]][modulo[c[i]]]++;

	}

	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!