您的位置:首页 > 其它

poj 3904 莫比乌斯函数灵活运用

2017-08-19 09:45 267 查看
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个数字,求其四个数字的子集的最大公约数是1的选法有多少种

题解:

利用莫比乌斯函数进行处理

最大公约数为1 的倍数

最大公约数为2的倍数

最大公约数为3的倍数

最大公约数为5的倍数这样处理下去

很明显就是莫比乌斯函数的运用了

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define LL long long
#define MAXN 10000
int cnt[MAXN+10],num[MAXN+10];
bool check[MAXN+10];
int primer[MAXN+10];
int mu[MAXN+10],n;

void Moblus()
{
memset(check,0,sizeof(check));
mu[1]=1;
int tot=0;
for(int i=2;i<=MAXN;i++){
if(!check[i]){
primer[tot++]=i;
mu[i]=-1;
}
for(int j=0;j<tot&&i*primer[j]<=MAXN;j++){
check[i*primer[j]]=true;
if(i%primer[j]==0){
mu[i*primer[j]]=0;
break;
}
mu[i*primer[j]]-=mu[i];
}
}
}

LL deal()
{
if(n<4) return 0;
LL ans=0;
for(int i=1;i<=MAXN;i++){
for(int j=1;j*i<=MAXN;j++)
num[i]+=cnt[j*i];
}

for(int i=1;i<=MAXN;i++){
if(num[i]>=4)
ans+=(LL)mu[i]*num[i]*(num[i]-1)*(num[i]-2)*(num[i]-3)/24;
}
return ans;
}

int main()
{
Moblus();
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int t;
memset(cnt,0,sizeof(cnt));
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++){
scanf("%d",&t);
cnt[t]++;
}
printf("%lld\n",deal());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: