Cod sursa(job #134222)

Utilizator recviemAlexandru Pana recviem Data 10 februarie 2008 22:35:58
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <stdio.h>   
using namespace std;   
  
    long long totient[1000000];   
    int n;   
    long long sum;   
  
void citire()   
{   
    freopen("fractii.in","r",stdin);   
    scanf("%d",&n);   
}   
  
void ciur()   
{   
    for (int i=4;i<n+1;i+=2)   
        totient[i]=totient[i]-totient[i]/2;   
    totient[2]=1;   
    for (int i=3;i<n+1;i+=2)   
    {   
        if (totient[i]==i)   
        {   
        for(int j=2*i;j<n+1;j+=i)   
            totient[j]=totient[j]-totient[j]/i;   
        totient[i]=i-1;   
        }   
    }   
}   
  
void scrie()   
{   
    freopen("fractii.out","w",stdout);   
    printf("%lld",sum);   
    fclose(stdout);   
}   
  
int main()   
{   
    citire();   
    for (int i=0;i<n+1;i++)   
        totient[i]=i;   
    for (int i=0;i<n+1;i++)   
        totient[i]=i;   
    sum = 1;   
    ciur();   
    for (int i=2;i<=n;i++)   
        sum += 2*totient[i];   
    scrie();   
    return 0;   
}