Cod sursa(job #2450889)

Utilizator AnimusFabian Animus Data 24 august 2019 19:27:29
Problema Fractii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <bitset>

using namespace std;

long long fractii[1000001];
int n;

int phi[1000001 + 1], prime[1000001/10], sz;
bitset <1000001 + 1> mark;

int main(){
    ifstream f("fractii.in");
    ofstream g("fractii.out");

    for (int i = 2; i <= 1000001; i++ ){
        if(!mark[i]){
            phi[i] = i-1;
            prime[sz++]= i;
        }
        for (int j=0; j<sz && prime[j]*i <= 1000001; j++ ){
            mark[prime[j]*i]=1;
            if(i%prime[j]==0){
                  phi[i*prime[j]] = phi[i]*prime[j];
                  break;
            }
            else phi[i*prime[j]] = phi[i]*(prime[j]-1 );
        }
    }
    f >> n;
    fractii[0] = 1;
    fractii[1] = 2;
    for(int i = 2; i <= n; i++){
        fractii[i] = fractii[i-1] + phi[i];
    }
    g << 2*fractii[n] - 3;
}