Cod sursa(job #2093301)

Utilizator AndreosAndrei Otetea Andreos Data 23 decembrie 2017 13:05:24
Problema Sum Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <cstdio>
#include <cmath>
#include <vector>
#include <bitset>
using namespace std;
bitset<200005> c;
bitset<200005> s;
vector<int> cc;
void ciur()
{
    int i,j;
    for(i=4; i<=200000; i+=2)
        c[i]=1;
    cc.push_back(2);
    for(i=3; i<=447; i+=2)
        if(c[i]==0)
            for(j=i*i; j<=200000; j=j+(i<<1))
                c[j]=1;
    for(i=3; i<=200000; i+=2)
        if(c[i]==0)
            cc.push_back(i);
}
int main()
{
    freopen("sum.in","r",stdin);
    freopen("sum.out","w",stdout);
    int n,i,j,x,cnt=1,xx;
    long long sol;
    scanf("%d",&n);
    ciur();
    for(i=1; i<=n; ++i)
    {
        scanf("%d",&x);
        xx=(x<<1);
        s[xx]=1;
        if(x%2==0)
        {
            for(j=2; j<=xx; j+=2)
                s[j]=1;
            while(x%2==0)
                x/=2;
        }
        while(x>=cc[cnt])
        {
            if(x%cc[cnt]==0)
            {
                s[cc[cnt]]=1;
                for(j=cc[cnt]*cc[cnt]; j<=xx; j+=(cc[cnt]<<1))
                    s[j]=1;
                while(x%cc[cnt]==0)
                    x/=cc[cnt];
            }
            cnt++;
        }
        if(x!=1)
        {
            s[x]=1;
            for(j=x*x; j<=xx; j+=(x<<1))
                s[j]=1;
        }
        sol=0;
        for(j=1; j<=xx; ++j)
            if(s[j]==1)
            {
                s[j]=0;
                sol-=j;
            }
        sol+=(xx*(xx+1)>>1);
        printf("%lld\n",sol);
    }
    return 0;
}