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. Python基础教程(020)--集成开发环境IDE简介--Pycharm

    前言 学会掌握Pycharm工具 内容 集成了开发软件需要的所有工具 1,图形用户界面 2,代码编译器(支持代码补全,自动缩进) 3,编译器,解释器 4,调试器(断点,单步执行) Pycharm介绍 ...

  2. 【靶场训练_DVWA】Command Execution

    low 利用: ;ls ../../ 源码分析: <?php if( isset( $_POST[ 'submit' ] ) ) { //将ip对应的值复制给target $target = $ ...

  3. window安装nodejs

    nvm管理nodejs 原文: https://www.cnblogs.com/shimily/articles/7244058.html1.下载nvm(nodejs版本管理工具) https://g ...

  4. sql:CallableStatement执行存储过程

    /** * 使用CablleStatement调用存储过程 * @author APPle * */ public class Demo1 { /** * 调用带有输入参数的存储过程 * CALL p ...

  5. Locust 源码理解与分析

    前言 相信很多小伙伴会选择Locust作为压测工具辅助测试,本文从Locust源码开始分析它的优劣,结论在最后,最终我还是选择了Jmeter 主要分析了Locust源码的两个文件:main.py 和 ...

  6. 116、TensorFlow变量的版本

    import tensorflow as tf v = tf.get_variable("v", shape=(), initializer=tf.zeros_initialize ...

  7. Tomcat安装后修改路径方法

    tomcat+mysql+jdk 本地已经安装tomcat.mysql.jdk服务,需要更改安装目录由D盘改为C盘,方法如下 一.停止tomcat.mysql服务 二.安装文件由D盘拷贝到C盘,原D盘 ...

  8. Pipenv管理项目环境--Django项目的一些最佳实践

    virtualenv --- 使用不方便 提升效率,管理更便捷--- pipenv 新建环境:: pip3 install pipenv 在项目下,用pipenv安装 Djagno pipenv in ...

  9. ZwQueryDirectoryFile用法

    1. 当返回值为STATUS_SUCCESS时,返回的字节数保存在IoStatusBlock.Information字段中: 2. 如果FileName字段被指定了,那么对于同时指定的FileHand ...

  10. Volatile 只保证可见性,并不保证原子性

    [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/52525724   在说明Java多线程内存可见性之前,先来简单了解一下J ...