Pagini recente » Cod sursa (job #1149459) | Cod sursa (job #1035382) | Cod sursa (job #2912058) | Cod sursa (job #1317754) | Cod sursa (job #3304402)
#include <fstream>
#include <algorithm>
using namespace std;
#define int long long
struct el {
int a, b, c;
};
ifstream in("puteri.in");
ofstream out("puteri.out");
int n;
int ans;
el v[100005];
int frec[130][130][130];
int mobius[205];
int rest[130][130];
void build_mobius() {
mobius[1] = -1;
for (int i = 1; i <= 128; i++) {
if (mobius[i]) {
mobius[i] = -mobius[i];
for (int j = 2 * i; j <= 128; j += i) {
mobius[j] += mobius[i];
}
}
}
for (int i = 1; i <= 128; i++) {
mobius[i] *= -1;
}
}
signed main() {
in >> n;
build_mobius();
// precompute remainders
for (int i = 0; i <= 128; i++)
for (int j = 2; j <= 128; j++)
rest[i][j] = i % j;
for (int i = 1; i <= n; i++) {
in >> v[i].a >> v[i].b >> v[i].c;
}
// inclusion-exclusion over mod i
for (int i = 2; i <= 128; i++) {
// build freq of residues mod i
for (int j = 1; j <= n; j++) {
frec[rest[v[j].a][i]][rest[v[j].b][i]][rest[v[j].c][i]]++;
}
long long sumprod = 0;
long long sum_self_sq = 0;
long long selfcomb = 0;
int lim = min(64LL, i - 1);
for (int a = 0; a <= lim; a++) {
for (int b = 0; b <= lim; b++) {
for (int c = 0; c <= lim; c++) {
int ra = i - a; if (ra == i) ra = 0;
int rb = i - b; if (rb == i) rb = 0;
int rc = i - c; if (rc == i) rc = 0;
long long f1 = frec[a][b][c];
long long f2 = frec[ra][rb][rc];
sumprod += f1 * f2;
if (a == ra && b == rb && c == rc) {
sum_self_sq += f1 * f1;
selfcomb += f1 * (f1 - 1) / 2;
}
}
}
}
long long rez = (sumprod - sum_self_sq) / 2 + selfcomb;
ans += rez * mobius[i];
// clear freq
for (int j = 1; j <= n; j++) {
frec[rest[v[j].a][i]][rest[v[j].b][i]][rest[v[j].c][i]]--;
}
}
out << ans;
return 0;
}