Cod sursa(job #2668072)

Utilizator metallidethantralayerIon Cojocaru metallidethantralayer Data 4 noiembrie 2020 14:12:44
Problema Sum Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;
int T,n;
int64_t S,S2;
vector <int64_t> Prim;
bitset <100005> V;
int main()
{
    freopen("sum.in","r",stdin);
    freopen("sum.out","w",stdout);
    Prim.push_back(2);
    for(int64_t i=3; i<=100000; i+=2)
    {
        if(!V[i])
        {
            for(int64_t j=2; i*j<=100000; j++)
                V[i*j]=true;
            Prim.push_back(i);
        }
    }
    cin>>T;
    while(T--)
    {
        cin>>n;
        n<<=1;
        int h=0,copie=n>>1;
        S=n*(n+1)/2,S2=0;
        vector <int64_t> P;
        while(copie>1)
        {
            if(!(copie%Prim[h]))
            {
                P.push_back(Prim[h]);
                while(!(copie%Prim[h]))
                    copie/=Prim[h];
            }
            h++;
            if(Prim[h]*Prim[h]>copie&&copie>1)
            {
                P.push_back(copie);
                break;
            }
        }
        for(int mask=1; mask<(1<<P.size()); mask++)
        {
            int64_t S1=1;
            int32_t k=__builtin_popcount(mask);
            for(int j=0; j<P.size(); j++)
                if(mask&(1<<j))
                    S1*=P[j];
            if(k&1)
                S2+=S1*(n/S1*(n/S1+1))/2;
            else
                S2-=S1*(n/S1*(n/S1+1))/2;

        }
        cout<<S-S2<<'\n';
    }
    return 0;
}