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. html 和 body标签的 css 设置

    个人猜测浏览器的机制:H5页面底板上有一张画布,画布高度可以被撑高.html.body等元素是固定在画布上的.浏览器中页面的滚动是跟着画布滚动的.(fixed定位是脱离这种机制的,相对浏览器窗口定位的 ...

  2. [CSP-S模拟测试]:u(差分)

    题目背景 $\frac{1}{4}$遇到了一道水题,完全不会做,于是去请教小$D$.小$D$看了一眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{4}$只好来问 ...

  3. Union比or快 Using UNION is faster when it comes to cases like scan two different column。

    problem: 595. Big Countries A country is big if it has an area of bigger than 3 million square km or ...

  4. xshell的安装及连接linux的使用方法

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lx_Frolf/article/deta ...

  5. Html5 学习笔记 【PC固定布局】 实战4 footer 区域

    最终效果图: Html代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta chars ...

  6. mysql + grafana监控

      1.首先需要增加授权 CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX' WITH MAX_USER_CONNECTIONS 3 ...

  7. 【Mock.js】前端模拟假数据,不用在手拼了

    [Mock.js]前端模拟假数据,不用在手拼了:https://www.jianshu.com/p/8579b703a4c1

  8. 偏向锁,偏向线程id ,自旋锁

    理解锁的基础知识 如果想要透彻的理解Java锁的来龙去脉,需要先了解以下基础知识. 基础知识之一:锁的类型 锁从宏观上分类,分为悲观锁与乐观锁. 乐观锁 乐观锁是一种乐观思想,即认为读多写少,遇到并发 ...

  9. 再往DjVu鼓吹者的头上敲一棒子

    最近在某论坛又看到有人在鼓吹DjVu,甚至声称拿到PDG就转成DjVu,忍不住想再敲打敲打. 早几年前就已经有人举出过实例,证明PDG.TIFF转DjVu会因为有损压缩而产生错别字,似乎时间长了一堆新 ...

  10. CSS div内放长英文字母或长数字自动换行 CSS一行排不下自动打断换行

    添加css  word-wrap:break-word 解释:使用break-word时,是将强制换行. 兼容各版本IE浏览器,兼容谷歌浏览器.