Cod sursa(job #2428370)

Utilizator ageneohasMart Florin ageneohas Data 4 iunie 2019 22:22:56
Problema Fractii Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.1 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include<fstream>
#include<vector>
#include<math.h>

using namespace std;
int isPrime(int a,vector<int> vect)
{
    for(int i=0;i<vect.size();i++)
    {
        if(a==vect.at(i))
            return 1;
    }
    return 0;
}
int main()
{
    int n;
    int * v = new int[1000000];
    int * nrPrime= new int[1000000];
    int * nrPrimeIndex = new int [10000];
    int * altVector = new int [1000000];
    int count=0;
    int numere=0;
    ifstream f("fractii.in");
    ofstream g("fractii.out");
    f>>n;
    for(int i=2;i<=n;i++)
    {
        if(v[i]==0)
        {
            nrPrime[i]=1;
            nrPrimeIndex[count]=i;
            count++;
            v[i]=1;
        }
        for(int j=i*i;j<=n;j+=i)
        {
            v[j]=1;
        }
    }
    for(int index=2;index<=n;index++)
    {
        if(nrPrime[index]==1)
        {
            numere=numere+(index-1);
        }
        else
        {
            int copie=index;
            int i=0;
            int k=0;
            while(copie>1)
            {
                if(copie%nrPrimeIndex[i]==0)
                while(copie%nrPrimeIndex[i]==0)
                {
                    altVector[nrPrimeIndex[i]]++;
                    copie=copie/nrPrimeIndex[i];
                }
                i++;
            }
            int produs=1;
            for(int parcurgere=2;parcurgere<=nrPrimeIndex[i];parcurgere++)
            {
                if(altVector[parcurgere]>0)
                {
                    produs=produs*((parcurgere-1)*(pow(parcurgere,altVector[parcurgere]-1)));
                    altVector[parcurgere]=0;
                }
            }
            numere=numere+produs;
        }
    }
    numere=numere*2+1;
    g<<numere;
    f.close();
    g.close();
    return 0;
}