Cod sursa(job #24822)

Utilizator cos_minBondane Cosmin cos_min Data 3 martie 2007 18:01:45
Problema Fractii Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
// 60 pcte
#include <stdio.h>
#include <fstream.h>
#include <string.h>

int maxim, v;
long long int val;
int ok[1000001], tot[1000001], p = 0, t;


int Cmmdc(int a, int b)
{
    if ( b == 0 ) return a; 
    return Cmmdc(b,a%b);
}    

int main()
{
    freopen("fractii.in","r",stdin);
    freopen("fractii.out","w",stdout);
    
    scanf("%d", &t);
    
    tot[2] = 1;
    tot[3] = 2;
    int j;
    for ( int n = 2; n <= t; n++ )
    {
         
         if ( tot[n] == 0 )
         {
             int i;
             int p = 0;
              for ( i = 2; i*i <= n; i++ )
              {
                  if ( n % i == 0 )
                  {
                      j = 1;
                      while  ( i*j <= t )
                      {
                          if( ok[i*j] == 0 && i*j <= n ) p++;
                          if ( i*j <= n ) ok[i*j] = 1;
                          if ( tot[n] != 0 && tot[i] != 0 ) tot[n*i] = tot[n]*tot[i];
         
                          if ( Cmmdc(i,j) == 1 && tot[j] != 0 && tot[i] != 0 ) 
                          {
                               tot[i*j] = tot[i]*tot[j];
                               if ( tot[n] != 0 ) tot[i*j*n] = tot[n*i*j];
                          }            
                          j++;
                      }        
                  }
             }
             if ( p == 0 ) tot[n] = n - 1;    
             if ( tot[n] == 0 ) tot[n] = n - p;   
             if ( n < t ) memset(ok, 0, n*sizeof(ok[0]));
        }    
        val += tot[n];  
     }
     val <<= 1;
     val += 1;
    printf("%lld",val);     
}