Cod sursa(job #2035920)

Utilizator dragos231456Neghina Dragos dragos231456 Data 9 octombrie 2017 22:47:41
Problema Fractii Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <bitset>
#include <cmath>
using namespace std;
ifstream f("fractii.in");
ofstream g("fractii.out");
bitset<1000005> ciur;
int n,m,r,v[1000005][3],fact[12],put[12],len,d;
long long rez;

void Eratosthenes()
{
    ciur[1]=1;
    for(int i=2;i<=m;++i)
    {
        r=n/i;
        for(int j=2;j<=r;++j)
        {
            ciur[i*j]=1;
            v[i*j][1]=i;
            v[i*j][2]=j;
        }
    }
}

void add(int x)
{
    bool ok=false;
    for(int i=1;i<=len;++i)
    {
        if(fact[i]==x)
        {
            ok=true;
            put[i]++;
        }
    }
    if(ok==false)
    {
        ++len;
        fact[len]=x;
        put[len]=1;
    }
}

void factprim(int n)
{
    if(ciur[n]==0)
    {
        add(n);
        return;
    }
    factprim(v[n][1]);
    factprim(v[n][2]);
}

int main()
{
    f>>n; m=sqrt(n);
    Eratosthenes();
    for(int i=2;i<=n;++i)
    {
        len=0; r=1;
        factprim(i);
        for(int j=1;j<=len;++j)
        {
            d=1;
            for(int h=1;h<=put[j];++h)
            {
                d*=fact[j];
            }
            r*=(d-(d/fact[j]));
        }
        rez+=(2*r);
    }
    ++rez;
    g<<rez;
    return 0;
}