Cod sursa(job #241153)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 9 ianuarie 2009 15:32:22
Problema Sum Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include<stdio.h>
#include<math.h>

int prim [ ] = { 0 , 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317 , 400 } ;

int main ( )
     {

        freopen ( "sum.in" , "r" , stdin ) ;
        freopen ( "sum.out" , "w" , stdout ) ;

        long n , i , x , j , lim ;

        scanf ( "%ld" , & n ) ;

        for ( i = 1 ; i <= n ; i ++ )
           {

              scanf ( "%ld" , & x ) ;
              long xx = x ;

              //factorizare

              j = 1 ;

              lim = sqrt ( x ) ;

              long long phi = x ;
              long e ;

              while ( prim [ j ] <= lim && x > 1 )
                   {
                      e = 0 ;

                      while ( x % prim [ j ] == 0 )
                          {
                             ++ e ;
                             x /= prim [ j ] ;
                          }
                      if ( e )
                         phi = ( phi / prim [ j ] ) * ( prim [ j ] - 1 ) ;
                      j ++ ;
                   }

              if ( x > 1 )
                  phi = ( phi / x ) * ( x - 1 ) ;

              printf ( "%lld\n" , ( xx * 2 ) * phi ) ;

           }


        return 0 ;

     }