Cod sursa(job #2519811)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 8 ianuarie 2020 14:29:48
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda RoadToIOI #6 Marime 2.16 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

ifstream fin("pairs.in");
ofstream fout("pairs.out");

void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;

inline int two(int n) { return 1 << n; }
inline int test(int n, int pos) { return (bool) (n & two(pos)); }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }

const int DMAX = 1e5+10;
const int LgMAX = 1e6+10;

struct nume{
    bool first;
    int second;
    vector <int> third;
};

int V[DMAX];

int n,maxim;

ll ans;

nume act[LgMAX];

int cauta(int val);
void ciur();
void pune(int val);

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t,i,j;

    fin>>n;
    for(i=1;i<=n;i++){
        fin>>V[i];
        maxim=max(V[i],maxim);
    }
    ciur();
    maxim=0;
    for(i=1;i<=n;i++){
        ans+=(i-1-cauta(V[i]));
        pune(V[i]);
        maxim=max(maxim,V[i]);

    }
    fout<<ans<<'\n';
    return 0;
}
void ciur(){
    ll i,j;
    act[0].first=true;
    for(i=2;i<=maxim;i++)
        if(!act[i].first){
           act[i].second=i;
           for(j=i*i;j<=maxim;j+=i){
               act[j].first=true;
               act[j].second=i;
           }
        }
}
int cauta(int val){
    int x;
    set <int> prime;
    while(val >= 2){
        for(auto& it:act[act[val].second].third)
            prime.insert(it);
        x=act[val].second;
        while(act[val].second == x)
              val/=x;
    }
    return prime.size();
}
void pune(int val){
    int x=val,y;
    while(val >= 2){
        act[act[val].second].third.pb(x);
        y=act[val].second;
        while(act[val].second == y)
             val/=y;
    }
}