POJ 3904 Sky Code --- 容斥原理

题目链接 https://vjudge.net/problem/POJ-3904

Sky Code

 POJ - 3904 

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

题意:给定n个正整数,问有多少个互素的四元组。

题解:容斥原理,先求有多少个不互素的四元组。

对输入的每个数进行质因数分解,走一遍容斥,把其质因数相乘的所有组合统计一次出现次数。

比如输入30,分解出来2,3,5。

说明30是2,3,5,6,15,10,30的倍数,所以用cnt[]数组记录下来每个因数出现的次数。

同时还要记录每个因数是由多少个素因数相乘得到的,一遍后面判断正负号。

统计完成后,遍历cnt数组,计算每个cnt的组合数C(cnt[i],4)(C可以提前打表算出来),

正负号根据前面的标记判断下即可。

( 题解思路参考自https://blog.csdn.net/water_zero_saber/article/details/79964496

#include <cstdio>
#include <cstring>
#define MAXN 10010
using namespace std;
typedef long long ll;
int input[MAXN];
int n;
int cnt[MAXN];
int prime[MAXN];
int flag[MAXN];
ll C[MAXN];// 计算C(n,4)
void Init() {
    for(int i = 4;i <= 10000;i++) {
        C[i] = (ll)i * (i-1) * (i-2) * (i-3) / 24;
    }
}

void Handle(int x) {
    // 分解质因数
    int temp = x;
    int num = 0;
    for(int i = 2;i * i <= temp;i++) {
        if(temp % i == 0) {
            prime[num++] = i;
        }
        while(temp % i == 0)
            temp /= i;
    }
    if(temp > 1)
        prime[num++] = temp;
    // 容斥原理
    int mul = 1,t;
    for(int i = 1;i < (1<<num);i++) {
        mul = 1,t = 0;
        for(int j = 0;j < num;j++) {
            if(i & (1<<j)) {
                mul *= prime[j];
                t++;
            }
        }
        cnt[mul]++;
        flag[mul] = t;
    }
        
}

int main()
{
    Init();
    while(scanf("%d",&n) != EOF) {
        memset(cnt,0,sizeof(cnt));
        memset(flag,0,sizeof(flag));
        for(int i = 0;i < n;i++) {
            scanf("%d",&input[i]);
            Handle(input[i]);
        }
        ll result = 0;
        for(int i = 2;i <= 10000;i++) {
            if(cnt[i] >= 4) {
                if(flag[i] & 1) result += C[cnt[i]]; 
                else result -= C[cnt[i]];
            }
        }
        printf("%lld\n",C[n] - result);
        
    }
    
    return 0;
} 

 

阅读更多
换一批

没有更多推荐了,返回首页