poj 2407 Relatives

这个是求互质的数的个数:

我们知道Eular n = a1^p1*a2^p2*...an^pn;

那么它的质因子个数为 num = (p1 + 1)*(p2 + 1) *...*( pn +1 );

那么它互质的数的个数为 num= ( 1 - 1/a1 )*(1 - 1/a2)*...*( 1 - 1/an )*n;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<map>

#include<cstring>

#include<vector>

using namespace std;

int prime[10000],cnt=0;

void Prime()

{

    bool hash[50024] = {0};

    int t = (int)sqrt(100000.0) + 1;

    for( int i = 3; i <= t ; i+=2 )

    {

         if( !hash[i>>1] )    

         {

             int x = i << 1;

             for( int j = i * i; j <=100001; j += x )

                  hash[j>>1] = true;

         }

    }

    prime[cnt++] = 2;

    for( int i = 1 ; i <= 50000 ; i++ )

    {

        if( !hash[i] )

        {

            prime[cnt++] = (i<<1) + 1;    

        }

    }

}

int Solve( int n )

{

    int ans = n;

    for( int i = 0; i < cnt ; i++ )

    {

         if( prime[i] > n ) break;

         if( n % prime[i] ==0 )

         {

             int t = 1;

             while( n % prime[i] ==0 )

             {

                    t++;

                    n /= prime[i];

             }

             ans = ans - ans/prime[i];        

         }    

    }

    if(  n > 1 )  ans = ans - ans/n;    

    return     ans;

}

int main(  )

{

     int n;

     Prime();

     while( scanf( "%d",&n ),n )

     {

           printf( "%d\n",Solve( n ) );    

     }

    //system( "pause" );

    return 0;

}

你可能感兴趣的:(relative)