Cod sursa(job #2522619)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 12 ianuarie 2020 18:52:39
Problema Puteri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <bits/stdc++.h>
using namespace std;

int rem[129], dp[65][65][65];
int mob[129], prm[129];

	
class InParser {
	
private:
	
	FILE *fin;
	
	char *buff;
	
	int sp;
	
 
	
	char read_ch() {
	
		++sp;
	
		if (sp == 4096) {
	
			sp = 0;
	
			fread(buff, 1, 4096, fin);
	
		}
	
		return buff[sp];
	
	}
	
 
	
public:
	
	InParser(const char* nume) {
	
		fin = fopen(nume, "r");
	
		buff = new char[4096]();
	
		sp = 4095;
	
	}
	
	
	
	InParser& operator >> (int &n) {
	
		char c;
	
		while (!isdigit(c = read_ch()) && c != '-');
	
		int sgn = 1;
	
		if (c == '-') {
	
			n = 0;
	
			sgn = -1;
	
		} else {
	
			n = c - '0';
	
		}
	
		while (isdigit(c = read_ch())) {
	
			n = 10 * n + c - '0';
	
		}
	
		n *= sgn;
	
		return *this;
	
	}
	
	
	
	InParser& operator >> (long long &n) {
	
		char c;
	
		n = 0;
	
		while (!isdigit(c = read_ch()) && c != '-');
	
		long long sgn = 1;
	
		if (c == '-') {
	
			n = 0;
	
			sgn = -1;
	
		} else {
	
			n = c - '0';
	
		}
	
		while (isdigit(c = read_ch())) {
	
			n = 10 * n + c - '0';
	
		}
	
		n *= sgn;
	
		return *this;
	
	}
	
} in("puteri.in");
ofstream out("puteri.out");

struct Struct {
	int a, b, c;
} arr[100005]; 

long long solve(int n, int k) {
	// optimization
	for (int i = 1; i <= 128; ++i) {
		rem[i] = rem[i - 1] + 1;
		if (rem[i] >= k)
			rem[i] -= k;
	}
	memset(dp, 0, sizeof dp);
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		int a = rem[k - arr[i].a], 
				b = rem[k - arr[i].b],
				c = rem[k - arr[i].c];
		if (a <= 64 and b <= 64 and c <= 64)
			ans += dp[a][b][c];
		++dp[rem[arr[i].a]][rem[arr[i].b]][rem[arr[i].c]];
	}
	return ans;
}

int main(void) {
	for (int i = 2; i <= 128; ++i)
		mob[i] = prm[i] = 1;
	for (int i = 2; i <= 128; ++i) {
		if (!prm[i])
			continue;
		for (int j = i; j <= 128; j += i) {
			prm[j] = 0;
			mob[j] *= -1;
		}
		prm[i] = 1;
		for (int j = i * i; j <= 128; j += i * i)
			mob[j] = 0;
	}
	int n;
	in >> n;
	for (int i = 1; i <= n; ++i)
		in >> arr[i].a >> arr[i].b >> arr[i].c;
	long long ans = 0;
	for (int i = 2; i <= 128; ++i)
		if (mob[i])
			ans += solve(n, i) * mob[i];	
	if (ans < 0)
		ans = -ans;
	out << ans;
	return 0;
}