Cod sursa(job #1741711)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 14 august 2016 20:50:24
Problema Puteri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <queue>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <set>
#include <map>
using namespace std;

const int MAXN = 100000;
const int MAXVAL = 128;
const int MAXEXP = 64;
int n, a[1 + MAXN], b[1 + MAXN], c[1 + MAXN];
int mobius[1 + MAXVAL], offset[1 + MAXVAL];
int dp[1 + MAXEXP][1 + MAXEXP][1 + MAXEXP];
bool prime[1 + MAXVAL];

long long Compute(int mod) {
    long long answer = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= MAXVAL; i++)
        offset[i] = i % mod;
    for (int i = 1; i <= n; i++) {
        int a0 = offset[mod - offset[a[i]]], b0 = offset[mod - offset[b[i]]], c0 = offset[mod - offset[c[i]]];
        if (a0 <= MAXEXP && b0 <= MAXEXP && c0 <= MAXEXP)
            answer += dp[a0][b0][c0];
        dp[offset[a[i]]][offset[b[i]]][offset[c[i]]]++;
    }
    return answer;
}

int main() {
    ifstream cin("puteri.in");
    ofstream cout("puteri.out");
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i] >> b[i] >> c[i];
    for (int i = 2; i <= MAXVAL; i++)
        mobius[i] = 1;

    long long answer = 0;
    for (int i = 2; i <= MAXVAL; i++) {
        if (!prime[i]) {
            for (int j = i; j <= MAXVAL; j += i) {
                mobius[j] *= -1;
                prime[j] = true;
            }
            for (int j = i * i; j <= MAXVAL; j += i * i)
                mobius[j] = 0;
        }
        if (mobius[i])
            answer -= mobius[i] * Compute(i);
    }
    cout << answer;
    return 0;
}