Cod sursa(job #3271006)

Utilizator poparobertpoparobert poparobert Data 25 ianuarie 2025 01:54:00
Problema Pairs Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef array<ll, 2> ll2;
typedef array<ll, 3> ll3;
typedef array<ld, 3> ld3;
typedef array<ll, 4> ll4;
typedef array<ll, 20> ll20;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define spit(x) return cout << x << '\n', void()
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> OS;
string ans[] = {"NO\n", "YES\n"};
const ll mod = 998244353;
void solve(int T){
    ll n, i, j, k, l, m, h, p, y, w, L;
    cin >> n;
    vector<ll> v(n + 1);
    vector<ll> ciur(1e6 + 1);
    vector<ll> mobius(1e6 + 1, 1ll);
    vector<ll> f(1e6 + 1);
    ciur[0] = ciur[1] = 1;
    for (i = 1; i <= n; i++){
        cin >> v[i];
        f[v[i]]++;
    }
    long long rez = n * (n - 1) / 2, ax;
    for (i = 2; i <= 1e6; i++){
        if (!ciur[i])
            mobius[i] = -1;
        for (j = i * 2; j <= 1e6; j += i)
            f[i] += f[j];
        ax = f[i];
        ax *= (ax - 1);
        ax /= 2;
        ax *= mobius[i];
        // if (i < 10)
        // cout << i << ' ' << f[i] << '' << mobius[i] << ' ' << ax<< endl;
        rez += ax;
        if (ciur[i]) continue;
        for (j = i * 2; j <= 1e6; j += i){
            ciur[j] = 1, mobius[j] *= mobius[i];
            if (j / i % i == 0) mobius[j] = 0;
        }
    }
    cout << rez;

}   
int main(){
    freopen("pairs.in", "r", stdin);
    freopen("pairs.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll T = 0;
    // ll t; cin >> t; while(t--)
        solve(++T);
    return 0;
}