POJ - 3904 Sky Code【莫比烏斯反演】

Sky Code

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3990   Accepted: 1337

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

題意:給N個不大於1e4的數,從中任取4個數,求GCD為1的取法有多少。

分析:直接上莫比烏斯套路。

設f(n)表示gcd為n的4元組的個數

設F(n)表示gcd為n的倍數的四元組個數,

設m為含有因子n的數的個數

那麽有:

F(n)=C_{m}^{4}

\because F(n)=\sum_{n|d}f(d)

\therefore f(n)=\sum _{n|d}u(d/n)F(d)

\therefore ans=\sum _{i=1}^{n}u(i)*F(i)]

//#include<bits/stdc++.h>

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <new>
#include <ctime>
#include <vector>

using namespace std;
long long mu[10004];
int prim[10004];
bool vis[10004];
int cnt = 0;

void init() {
    memset(prim, 0, sizeof(prim));
    memset(mu, 0, sizeof(mu));
    memset(vis, 0, sizeof(vis));
    mu[1] = 1;
    for (int i = 2; i < 10004; ++i) {
        if (!vis[i]) {
            prim[cnt++] = i;
            mu[i] = -1;
        }
        for (int j = 0; j < cnt && i * prim[j] < 10004; ++j) {
            vis[i * prim[j]] = 1;
            if (i % prim[j] == 0)break;
            else mu[i * prim[j]] = -mu[i];
        }
    }
}

long long F[10004];
long long a[10004];

int main() {
    init();
    int n;
    while (cin >> n) {
        long long maxi = 0;
        for (int i = 0; i < n; ++i) {
            scanf("%lld", &a[i]);
            maxi = max(maxi, a[i]);
        }
        memset(F, 0, sizeof(F));

        for (int i = 0; i < n; ++i) {
            for (int j = 1; j * j <= a[i]; ++j) {
                if (a[i] % j == 0) {
                    F[j]++;
                    if (j * j != a[i])F[a[i] / j]++;
                }
            }
        }
        long long ans = 0;
        for (int i = 1; i <= maxi; ++i) {
            if (F[i] >= 4);
            ans += mu[i] * (F[i] - 3) * (F[i] - 2) * (F[i] - 1) * F[i] / 24;
        }
        printf("%lld\n", ans);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/89161719
sky