SPOJ_4191 Sky Code

http://www.spoj.pl/problems/MSKYCODE/

题意:

有N个数,让你从中选出4个,使得它们的最大公约数为1 。N<=10000

思路:

用容斥原理来统计数。我们考虑问题的反面,就是先求出所有可能4个数的取法,然后减去4个数的

最大公约数不为1的组合。

#include<stdio.h>
#include<string.h>
typedef long long LL ;
const int MAXN = 10010 ;
LL  N ;
LL d[MAXN] , num[MAXN] , cnt[MAXN];

LL cal( LL n ){
    LL res = n * (n - 1LL) * (n - 2LL) * (n - 3LL) / (24LL) ;
    return res ;
}

int main(){
    LL a;
    while( scanf("%lld",&N) == 1){
        for(int i=1;i<MAXN;i++){
            d[i] = 0 ;      //有多少个数能被i整除
            num[i] = 0 ;    //值为i的数的个数
            cnt[i] = 0 ;    //约数为i的数被计算了几次
        }
        for(int i=1;i<=N;i++){
            scanf("%lld",&a);
            num[a] ++ ;
        }
        if( N < 4 ){
            printf("0\n");  continue ;
        }
        for(LL i=2;i<MAXN;i++){
            for(LL j=i;j<MAXN;j+=i){
                d[i] += num[j] ;
            }
        }
        LL tot = 0;
        for(LL i=2;i<MAXN;i++){
            LL a = 1 - cnt[i] ;
            tot += a * cal( d[i] ) ;
            for(int j=2*i;j<MAXN;j+=i){
                cnt[ j ] += a ;
            }
        }
        printf("%lld\n", cal(N) - tot ) ;
    }
    return 0 ;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值