SPOJ_4191 Sky Code

来源:互联网 发布:python使用c数据结构 编辑:程序博客网 时间:2024/06/08 12:08

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 ;}


原创粉丝点击