Cod sursa(job #3304393)

Utilizator unomMirel Costel unom Data 23 iulie 2025 10:59:50
Problema Puteri Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <fstream>

using namespace std;

ifstream in("puteri.in");
ofstream out("puteri.out");
int n;
long long ans;
short frec[65][64][64][64];
int mobius[105];
short rest[65][65];

void build_mobius()
{
    mobius[1] = -1;

    for(int i = 1; i<=64; i++)
    {
        if(mobius[i])
        {
            mobius[i] = -mobius[i];

            for(int j = 2 * i; j<=64; j += i)
            {
                mobius[j] += mobius[i];
            }
        }
    }

    for(int i = 1; i<=64; i++)
    {
        mobius[i] *= -1;
    }
}

int main()
{
    in>>n;

    build_mobius();

    for(int i = 2; i<=64; i++)
    {
        for(int j = 0; j<=64; j++)
        {
            rest[i][j] = j % i;
        }
    }

    int a, b, c;
    for(int i = 1; i<=n; i++)
    {
        in>>a>>b>>c;

        long long rez = 0;
        for(int j = 2; j<=64; j++)
        {
            int ra = j - rest[j][a];
            int rb = j - rest[j][b];
            int rc = j - rest[j][c];

            if(ra == j)
            {
                ra = 0;
            }
            if(rb == j)
            {
                rb = 0;
            }
            if(rc == j)
            {
                rc = 0;
            }

            rez += 1LL * frec[j][ra][rb][rc] * mobius[j];

            //out<<rez<<" ";
        }
        //out<<'\n';

        ans += rez;

        for(int j = 2; j<=64; j++)
        {
            frec[j][rest[j][a]][rest[j][b]][rest[j][c]]++;
        }
    }

    out<<ans;

    return 0;
}