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

Uva 10325 The Lottery ( 容斥原理 )

时间:2014-11-01 17:27:35      阅读:153      评论:0      收藏:0      [点我收藏+]

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

 

Uva 10325 The Lottery ( 容斥原理 )

 

bubuko.com,布布扣

 

bubuko.com,布布扣
#include <cstdio>
#include <cstring>
typedef long long LL;
LL x[20],n, m;

LL gcd( LL a, LL b )
{
    return ( b == 0 ) ? a : gcd( b, a % b );
}
LL lcm( LL a, LL b )
{
    return a / gcd( a, b ) * b;
}

void solve()
{
    LL ans = 0;
    for( int i = 1; i < ( 1 << m ); ++i )
    {
        LL mult = 1;
        LL bits = 0;
        for( int j = 0; j < m; ++j )
        {
            if( i & ( 1 << j ) )
            {
                mult = lcm( mult, x[j] );
                if( mult > n ) break;
                bits++;
            }
        }
        if( mult > n ) continue;
        if( bits & 1 )    ans += n / mult;
        else            ans -= n / mult;
    }
    printf( "%lld\n", n - ans );
}

int main()
{
    while( ~scanf( "%lld %lld", &n, &m ) )
    {
        for( int i = 0; i < m; ++i )
            scanf( "%lld", &x[i] );
        solve();
    }
    return 0;
}
代码君

 

Uva 10325 The Lottery ( 容斥原理 )

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

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

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