POJ 3904 (莫比乌斯反演)

博客包含Input、Output、Sample Input、Sample Output等内容,可能与程序的输入输出示例有关,信息较少,未体现更多关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个数,让你从中选出四个求出gcd(a,b,c,d)=1的对数

思路:莫比乌斯反演
首先莫比乌斯反演有两种形式,
反演公式一 f(n) = 累加(d|n) mu(d)*F(n/d)
反演公式二 f(n) = 累加(n|d) mu(d/n)*F(d)

我们设 F(n)为 gcd(a,b,c,d)==n的倍数 的对数
我们设 f(n)为 gcd(a,b,c,d)==n    的对数

那我们就是要求f(1),那就相当于 f(1) = 累加(1-n)mu(d)*F(d)
F(n) 即我求出所有数中有多少个是n个倍数即可,然后求出C(m,4)即是答案

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#define maxn 100005
#define mod 1000000007
using namespace std;
typedef long long ll;
ll n;
ll mu[maxn+10];
ll vis[maxn+10];
ll a[maxn+10];
ll tot[maxn+10];
void init(){
    for(int i=0;i<maxn;i++){
        vis[i]=0;
        mu[i]=1;
    }
    for(int i=2;i<maxn;i++){
        if(vis[i]==0){
            mu[i]=-1;
            for(int j=2*i;j<maxn;j+=i){
                vis[j]=1;
                if((j/i)%i==0) mu[j]=0;
                else mu[j]*=-1;
            }
        }
    }
}
void get(){
    for(int i=0;i<n;i++){
        ll x=a[i];
        ll t=sqrt(x); 
        for(int j=1;j<=t;j++){
            if(x%j==0){
                tot[j]++;
                if(x/j!=j) tot[x/j]++;  
            }
        }
    }
}
ll C(ll x){
    if(x==0) return 0;
    return x*(x-1)*(x-2)*(x-3)/24; 
}
int main(){
    init();
    while(scanf("%lld",&n)!=EOF){
        memset(tot,0,sizeof(tot));
        for(int i=0;i<n;i++){
            scanf("%lld",&a[i]);
        }
        get();
        ll sum=0;
        for(int i=1;i<=maxn;i++){
            sum+=mu[i]*C(tot[i]);
        }
        printf("%lld\n",sum);
    }
    return 0;
}

 



转载于:https://www.cnblogs.com/Lis-/p/11180001.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值