Cod sursa(job #112804)

Utilizator Poisoned_IvyAnda Nicolae Poisoned_Ivy Data 7 decembrie 2007 20:41:55
Problema Fractii Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <stdio.h>
#include <malloc.h>
unsigned long n;
int* totient;


unsigned long calcul(int n)
{
         int i,j,s=0;
         for (i=0; i<=n; i++)
             totient[i]=1;
         for (i=2; i<n; i++)
             for (j=i+1; j<=n; j++)
                 if ((j%i==0) && (totient[i]==1)) 
                 {
                    if (totient[j]>1) totient[j]=totient[j]*(i-1)/i;
                    else totient[j]=j*(i-1)/i;
                 }
         for (i=2; i<=n; i++)
         {
             if (totient[i]==1) totient[i]=i-1;
             s+=totient[i];
         }
         return s;
}


int main()
{
    FILE *f, *g;
    f=fopen("D:\\fractii.in","r");
    fscanf(f,"%lu",&n);
    totient=(int*) malloc ((n+1)*sizeof(int));
    g=fopen("D:\\fractii.out","w");
    fprintf(g,"%lu",2*calcul(n)+1);
    return 0;
}