Cod sursa(job #1874139)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 9 februarie 2017 18:47:12
Problema Sum Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
# include <iostream>
# include <fstream>

# include <vector>

using namespace std;

inline long long gauss( int n ) {
    return n * ( n + 1 ) / 2;
}

inline int apply_mask( int mask, const vector<int> &t ) {
    long long p = 1;

    for ( int x : t ) {
        p *= ( mask & 1 ? x : 1 );
        mask >>= 1;
    }

    return p;
}

vector<int> prime_factors( int x ) {
    vector<int> f;

    if ( x % 2 == 0 ) {
        f.push_back( 2 );
        do {
            x /= 2;
        } while ( x % 2 == 0 );
    }

    int d = 3;
    while ( d * d <= x ) {
        if ( x % d == 0 ) {
            f.push_back( d );
            do {
                x /= d;
            } while ( x % d == 0 );
        }

        d += 2;
    }

    if ( x > 1 )
        f.push_back( x );

    return f;
}


int main() {
    ifstream fin( "sum.in" );
    ofstream fout( "sum.out" );

    int n;
    fin >> n;

    for ( int i = 0; i < n; i ++ ) {
        int x;
        fin >> x;

        vector<int> f = prime_factors( x );
        long long s = gauss( 2 * x );

        for ( int mask = 1; mask < ( 1 << f.size() ); mask ++ ) {
            int p = apply_mask( mask, f );
            s += p * gauss( 2 * x / p ) * ( __builtin_popcount( mask ) & 1 ? -1 : 1 );
        }

        fout << s << '\n';
    }

    fin.close();
    fout.close();

    return 0;
}