HDU 4135 Co-prime

简单的容斥定理的运用:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
#include<string>
#define LL long long
using namespace std;
int prime[10000],cnt=0;
LL factor[40];
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 <= 100000 ; 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 Get_Factor( LL num  )
{
    int count = 0;
    for( int i = 0 ; i < cnt ; i ++ )
    {
         if( num < prime[i] ) break;
         if( num % prime[i] == 0 )
         {
             factor[count++] = prime[i];
             while( num % prime[i] == 0 )
                    num /= prime[i];
         }    
    }    
    if( num != 1 ) factor[count++] = num;
    return count;
}
LL DFS( LL num , int start ,int count)
{
    LL ans = 0;
    for( int i = start ; i < count ; i ++ )
         ans += num /(LL) factor[i] - DFS( num /(LL) factor[i] , i + 1 , count );
    return ans;    
}
int main(  )
{
    Prime();
    int T;
    LL A,B,N;
    while( scanf( "%d",&T )==1 )
    {
         for( int i = 1 ;i <= T ; i ++ )
         {
              
              LL ans1 = 0,ans2 = 0;
              scanf( "%I64d %I64d %I64d",&A,&B,&N );
              if( N == 1 ) {
                    printf( "Case #%d: %I64d\n",i,B -A +1 );
                    continue;
              }
              int count = Get_Factor( N );    
              ans1 = B  - DFS( B , 0 ,count );
              ans2 = A - 1  - DFS( A-1 , 0, count );
              printf( "Case #%d: %I64d\n",i,ans1 - ans2 );
         }    
    }
    //system( "pause" );
    return 0;
}

 

posted @ 2012-07-27 21:57  wutaoKeen  阅读(198)  评论(0)    收藏  举报