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

POJ 3904 Sky Code (容斥+莫比乌斯反演)

时间:2015-08-16 15:16:30      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:poj   莫比乌斯反演   容斥   


Sky Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1831   Accepted: 570

Description

Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing to steal the spacecraft of Petru. There is only one problem – Petru has locked the spacecraft with a sophisticated cryptosystem based on the ID numbers of the stars from the Milky Way Galaxy. For breaking the system Stancu has to check each subset of four stars such that the only common divisor of their numbers is 1. Nasty, isn’t it? Fortunately, Stancu has succeeded to limit the number of the interesting stars to N but, any way, the possible subsets of four stars can be too many. Help him to find their number and to decide if there is a chance to break the system.

Input

In the input file several test cases are given. For each test case on the first line the number N of interesting stars is given (1 ≤ N ≤ 10000). The second line of the test case contains the list of ID numbers of the interesting stars, separated by spaces. Each ID is a positive integer which is no greater than 10000. The input data terminate with the end of file.

Output

For each test case the program should print one line with the number of subsets with the asked property.

Sample Input

4
2 3 4 5 
4
2 4 6 8 
7
2 3 4 5 7 6 8

Sample Output

1 
0 
34

Source

Southeastern European Regional Programming Contest 2008

题目链接:http://poj.org/problem?id=3904

题目大意:给n个不相同的数,从中任意选出4个,使得它们的最大公约数为1,问有多少种选法

题目分析:首先n小于4肯定是0了,C(n,4) = n * (n - 1) * (n - 2) * (n - 3) / 24,本题数据范围不大,long long即可,差不多是裸的莫比乌斯反演题了,和NOJ 2079几乎一样,ac代码在POJ上rank 7

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e4 + 5;
int mob[MAX], p[MAX], cnt[MAX], num[MAX];
bool prime[MAX];
int n, ma;

void Mobius()
{
    memset(prime, true, sizeof(prime));
    int pnum = 0;
    mob[1] = 1;
    for(int i = 2; i < MAX; i++)
    {
        if(prime[i])
        {
            p[pnum ++] = i;
            mob[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < MAX; j++)
        {
            prime[i * p[j]] = false;
            if(i % p[j] == 0)
            {
                mob[i * p[j]] = 0;
                break;
            }
            mob[i * p[j]] = -mob[i];
        }
    }
}

ll cal()
{
    for(int i = 1; i <= ma; i++)
        for(int j = i; j <= ma; j += i)
            num[i] += cnt[j];
    ll ans = 0;
    for(int i = 1; i <= ma; i++)
    {
        int x = num[i];
        if(x >= 4)
            ans += (ll) mob[i] * x * (x - 1) * (x - 2) * (x - 3) / 24;
    }
    return ans;
}

int main()
{
    Mobius();
    while(scanf("%d", &n) != EOF)
    {
        ma = 0;
        memset(cnt, 0, sizeof(cnt));
        memset(num, 0, sizeof(num));
        for(int i = 0; i < n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            cnt[tmp] ++;
            ma = max(ma, tmp);
        }
        if(n < 4)
        {
            printf("0\n");
            continue;
        }
        printf("%lld\n", cal());
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3904 Sky Code (容斥+莫比乌斯反演)

标签:poj   莫比乌斯反演   容斥   

原文地址:http://blog.csdn.net/tc_to_top/article/details/47702021

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