码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4135 Co-prime ( 容斥原理 )

时间:2014-10-31 23:29:51      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   for   sp   

 

HDU 4135 Co-prime ( 容斥原理 )

 

bubuko.com,布布扣

 

bubuko.com,布布扣
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
#define MAXN 100
int fac[ MAXN ], cnt;

void gaoji( int x )    //对x质因数分解 
{
    cnt = 0;
    for( int i = 2; i * i <= x; )
    {
        if( x % i == 0 )
        {    
            fac[ cnt++ ] = i;
            while( x % i == 0) x /= i;
        }
        if( i == 2 ) i++;
        else i += 2;
    }
    if( x > 1 )
        fac[ cnt++ ] = x;
}

LL solve( LL x )
{
    LL res = 0;
    for( LL i = 1; i < ( 1 << cnt ); ++i ) //二进制压缩 
    {
        LL mul = 1, bits = 0;
        for( LL j = 0; j < cnt; ++j )
        {
            if( ( i >> j ) & 1 )
            {
                ++bits;
                mul *= fac[j];
            }
        }
        if( bits & 1 )    res += x / mul;
        else        res -= x / mul;
    }
    return x - res;
} 

void Orz()
{
    int t, cas = 1, n;
    scanf( "%d", &t );
    while( t-- )
    {
        LL a, b;
        scanf( "%I64d %I64d %d", &a, &b, &n );
        gaoji( n );
        LL ans = solve( b ) - solve( a - 1 );
        printf( "Case #%d: %I64d\n", cas++, ans );
    }
}

int main()
{
    Orz();    
    return 0;
}
代码君

 

HDU 4135 Co-prime ( 容斥原理 )

标签:style   blog   http   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/BigBallon/p/4066141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!