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+];
ll vis[maxn+];
ll a[maxn+];
ll tot[maxn+];
void init(){
for(int i=;i<maxn;i++){
vis[i]=;
mu[i]=;
}
for(int i=;i<maxn;i++){
if(vis[i]==){
mu[i]=-;
for(int j=*i;j<maxn;j+=i){
vis[j]=;
if((j/i)%i==) mu[j]=;
else mu[j]*=-;
}
}
}
}
void get(){
for(int i=;i<n;i++){
ll x=a[i];
ll t=sqrt(x);
for(int j=;j<=t;j++){
if(x%j==){
tot[j]++;
if(x/j!=j) tot[x/j]++;
}
}
}
}
ll C(ll x){
if(x==) return ;
return x*(x-)*(x-)*(x-)/;
}
int main(){
init();
while(scanf("%lld",&n)!=EOF){
memset(tot,,sizeof(tot));
for(int i=;i<n;i++){
scanf("%lld",&a[i]);
}
get();
ll sum=;
for(int i=;i<=maxn;i++){
sum+=mu[i]*C(tot[i]);
}
printf("%lld\n",sum);
}
return ;
}
												

POJ 3904 (莫比乌斯反演)的更多相关文章

  1. POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数

    http://poj.org/problem?id=3904   题意:给一些数,求在这些数中找出四个数互质的方案数.   莫比乌斯反演的式子有两种形式http://blog.csdn.net/out ...

  2. poj 3904(莫比乌斯反演)

    POJ 3904 题意: 从n个数中选择4个数使他们的GCD = 1,求总共有多少种方法 Sample Input 4 2 3 4 5 4 2 4 6 8 7 2 3 4 5 7 6 8 Sample ...

  3. UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.

    题意: 这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N 分析: 坐标轴上有四个可见的点,因为每个象限可见的点数都是一样 ...

  4. POJ 3904

    第一道莫比乌斯反演的题. 建议参看http://www.isnowfy.com/mobius-inversion/ 摘其中部分 证明的话感觉写起来会比较诡异,大家意会吧说一下这个经典题目:令R(M,N ...

  5. hdu1695 GCD(莫比乌斯反演)

    题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...

  6. BZOJ 2154: Crash的数字表格 [莫比乌斯反演]

    2154: Crash的数字表格 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 2924  Solved: 1091[Submit][Status][ ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)

    题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...

  9. 莫比乌斯函数筛法 & 莫比乌斯反演

    模板: int p[MAXN],pcnt=0,mu[MAXN]; bool notp[MAXN]; void shai(int n){ mu[1]=1; for(int i=2;i<=n;++i ...

随机推荐

  1. AcWing 260. 买票 (树状数组+二分)打卡

    题目:https://www.acwing.com/problem/content/description/262/ 题意:给定一个队伍,每个人过来的时候可以插队,每个人会输入一个插入到哪个位置,但是 ...

  2. fiddler使用笔记1

    转载地址:写得很不错的fildder教程   http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站:  www.fiddler2.c ...

  3. [CSP-S模拟测试]:Set(随机化)

    题目描述 你手上有$N$个非负整数,你需要在这些数中找出一个非空子集,使得它的元素之和能被$N$整除.如果有多组合法方案,输出任意一组即可.注意:请使用高效的输入输出方式避免输入输出耗时过大. 输入格 ...

  4. 关于设置shadowPath的重要性

    这是超级容易添加阴影到iOS中的任何视图.所有您需要做的是 添加QuartzCore框架到项目中(如果不存在的话) 导入QuartzCore到您的执行文件 添加一行如[myView.layer set ...

  5. 自动收集有效IP代理

    自动收集有效IP代理 #需要的外部依赖包requests和lxml#自动获取的代理ip数据保存为”IP代理池.txt“#read_ip函数用于提取”IP代理池.txt“的数据返回类型为列表from l ...

  6. Linux 下查看用户组信息

    1.id 通过id 可以直接查看到当前登陆用户的uid, group id , group name 等等.. 2.groups

  7. python导入自定义模块和包

    参考资料 https://blog.csdn.net/gvfdbdf/article/details/52084144 http://www.runoob.com/python/python-modu ...

  8. Cocos2d之FlyBird开发---简介

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 开发FlyBird其实非常的简单,在游戏的核心部分,我们需要实现的只有: 创建一个物理世界(世界设置重力加速度) 在物理世界中添加一个动态 ...

  9. docker安装中遇到的问题

    错误一 提示:Segmentation Fault or Critical Error encountered. Dumping core and aborting. Aborted 解答:安装错误安 ...

  10. Oracle学习笔记<3>

    单值函数 1.函数的分类 Oracle数据库中函数分为两类: 1)单值函数 n条数据经过函数处理得到n条结果 例如:查询所有员工last_name,并以全部大写形式输出 2)多值函数(组函数) n条数 ...