[POJ3904]Sky Code

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

 
 

 
 
题目大意:给你n个数,让你找出来4个数使得gcd(a, b, c, d)=1,问你有多少组。
其实这道题可以用莫比乌斯反演推公式出来的,但我比较蒟蒻直接用莫比乌斯函数容斥。
我们把题意转化一下,就是求总方案数-gcd不是1的方案数。
总方案数就是$\large \textrm{C}_{n}^{4}$。
然后减去gcd=2的个数,减去gcd=3的个数,这样6被减了两次,所以加上gcd=6的个数。
我们发现这些前面的系数就是莫比乌斯函数,有奇数个质因子的的是-,有偶数个质因子的是+。
所以直接求...
记得开long long
 

 
 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
using namespace std;
#define int long long
#define gc getchar()
inline int read(){
    int res=0;char ch=gc;
    while(!isdigit(ch))ch=gc;
    while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch^48);ch=gc;}
    return res;
}
#undef gc
int n;
int prime[10005], top, miu[10005];
bool vis[10005];
int num[100005], cnt[100005];

signed main()
{
    miu[1] = 1;
    for (int i = 2 ; i <= 10000 ; i ++)
    {
        if (!vis[i]) {prime[++top] = i, miu[i] = -1;}
        for (int j = 1 ; j <= top and i * prime[j] <= 10000; j ++)
        {
            vis[i*prime[j]] = 1;
            if (i % prime[j] == 0) break;
            miu[i*prime[j]] = -miu[i];
        }
    }
    while(scanf("%lld", &n) != EOF)
    {
        memset(num, 0, sizeof num), memset(cnt, 0, sizeof cnt);
        for (int i = 1 ; i <= n ; i ++)
            num[read()]++;
        if (n < 4) {puts("0");continue;}
        int ans = 0;
        for (int i = 1 ; i <= 10000 ; i ++)
            for (int j = i ; j <= 10000 ; j += i)
                cnt[i] += num[j]; //gcd为i的集合中数的个数 
        for (int i = 1 ; i <= 10000 ; i ++)
            if (cnt[i] >= 4) ans += miu[i] * cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) * (cnt[i] - 3) / 24;
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zZh-Brim/p/9398788.html
今日推荐