Cod sursa(job #1704346)

Utilizator Debuger33Numarul1 Debuger33 Data 18 mai 2016 17:11:10
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>

using namespace std ;
 
ifstream fin("pairs.in") ;
ofstream fout("pairs.out") ;
 
int mobius[1000000 + 10] ;
 
void umple(int x){
    mobius[0] = 0 ; mobius[1] = 1 ;
    for(int i = 2 ; i <= x ; i ++){
        mobius[i] = - 1 ;
    }
    for(int i = 2 ; i <= x ; i ++){
        if(mobius[i]){
            for(int j = i + i ; j <= x ; j += i){
                mobius[j] -= mobius[i] ;
            }
        }
    }
}
 
int ap[1000000 + 10] , divi[1000000 + 10] ;
int main(){
    int max = 0 ;
    int n , x ;
    fin >> n ;
    for(int i = 1 ; i <= n ; i ++){
        fin >> x ;
        ap[x] ++ ;
        if(max < x){
            max = x ;
        }
    }
    divi[1] = n ;
    long long rez = 0 ;
    umple(max) ;
    for(int d = 2 ; d <= max ; d ++){
        if(mobius[d]){
		for(int j = d ; j <= max ; j += d){
			divi[d] += ap[j] ;
		}
           	rez += 1LL * mobius[d] * ((1LL * divi[d] * (divi[d] - 1)) / 2) ;
        }
    }
    rez += (1LL * n * (n - 1)) / 2 ;
    fout << rez ;
    fout.close() ;
    fin.close() ;
    return 0 ;
}