HDU 1796 How many integers can you find

容斥定理:

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;

LL num[124],ans;  

LL Gcd( LL a, LL b )

{

   return b == 0 ? a : Gcd( b , a%b ); 

} 

void DFS( LL d , int t , LL lcm ,int cnt , int c )

{

   lcm = num[t]*lcm/Gcd( lcm ,num[t] );

   if( c&1 ) ans += d / lcm;

   else ans -= d / lcm;

   for( int i = t + 1 ; i < cnt ; i ++ )

        DFS( d , i, lcm , cnt , c + 1  );

} 

int main(  )

{

     int  m;LL n; 

     while( scanf( "%I64d %d",&n ,&m )==2 )

     {

           bool flag = 0;

           int cnt = 0;    

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

           { 

                scanf( "%I64d",&num[i] );

                if( num[i] != 0 ) num[cnt++] = num[i]; 

           } 

           ans = 0;

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

           {

                DFS( n-1  , i ,num[i] ,cnt ,1 ); 

           }

           printf( "%I64d\n",ans  );        

     } 

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(Integer)