Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1.

Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109 + 7.

Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1].

Input

The first line contains one integer number n (1 ≤ n ≤ 100000).

The second line contains n integer numbers a1, a2... an (1 ≤ ai ≤ 100000).

Output

Print the number of coprime subsequences of a modulo 109 + 7.

Examples

Input
3
1 2 3
Output
5
Input
4
1 1 1 1
Output
15
Input
7
1 3 5 15 3 105 35
Output
100

题意:给定N个数,问有多少个子序列,其GCD=1。

思路:我们枚举GCD=g的倍数,那么是是g的倍数的个数为X的时候,其贡献是pow(2,X)-1。加上容斥,前面加一个莫比乌斯系数即可。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int maxn=;
const int Mod=1e9+;
int num[maxn],p[maxn],vis[maxn],mu[maxn],cnt,ans;
vector<int>G[maxn];
int qpow(int a,int x){
int res=; while(x){
if(x&) res=(ll)res*a%Mod;
x>>=; a=(ll)a*a%Mod;
} return res;
}
void prime()
{
mu[]=; rep(i,,maxn-) G[i].push_back();
for(int i=;i<maxn;i++){
if(!vis[i]) p[++cnt]=i,mu[i]=-;
for(int j=i;j<maxn;j+=i) G[j].push_back(i);
for(int j=;j<=cnt&&p[j]*i<maxn;j++){
mu[i*p[j]]=-mu[i]; vis[i*p[j]]=;
if(i%p[j]==){mu[i*p[j]]=; break;}
}
}
}
int main()
{
prime() ;int N,x;
scanf("%d",&N);
rep(i,,N){
scanf("%d",&x);
rep2(j,,G[x].size()) num[G[x][j]]++;
}
rep(i,,) ans=((ans+mu[i]*(qpow(,num[i])-))%Mod+Mod)%Mod;
printf("%d\n",ans);
return ;
}

CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)的更多相关文章

  1. Codeforces 803F Coprime Subsequences (容斥)

    Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[ ...

  2. Codeforces 803F - Coprime Subsequences(数论)

    原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...

  3. CodeForces 803F Coprime Subsequences

    $dp$. 记$dp[i]$表示$gcd$为$i$的倍数的子序列的方案数.然后倒着推一遍减去倍数的方案数就可以得到想要的答案了. #include <iostream> #include ...

  4. [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]

    题面 传送门 思路 新姿势get 莫比乌斯容斥 $\sum_{i=1}{n}\mu(i)f(i)$ 这个东西可以把所有没有平方质因子的东西表示出来,还能容斥掉重复的项 证明是根据莫比乌斯函数的定义,显 ...

  5. CF(439E - Devu and Birthday Celebration)莫比乌斯容斥

    题意:将n个糖果插入f-1个挡板分成f分(a1,a2,a3...af). 问有多少种分法能够使得gcd(a1,a2,a3...af)=1; 解法.莫比乌斯容斥,首先按1为单位分,这时候有C(n-1,f ...

  6. Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)

    大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其 ...

  7. HihoCoder - 1867: GCD (莫比乌斯容斥)

    Sample Input 6 1 6 2 5 3 4 Sample Output 10 You are given a {1, 2, ..., n}-permutation a[1], a[2], . ...

  8. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

随机推荐

  1. solr、Lucene、IKAnalyzer这三者关系是怎样的?

    lucene 是开源搜索引擎 solr 是基于 lucene开发的搜索引擎 IK 是中文分词. lucene 不是一个搜索引擎,只是一个基础的文件索引工具包,或者叫“搜索引擎开发包”.不能单独作为程序 ...

  2. 014_HDFS存储架构、架构可靠性分析、副本放置策略、各组件之间的关系

    1.HDFS存储架构

  3. MySQL-5.7权限详解

    1.MySQL权限级别 (1)全局性管理权限 作用于整个MySQL实例级别 *.*代表所有数据库的权限 mysql> grant all on *.* to 'test'@'%'; Query ...

  4. samtools+bcftools 进行SNP calling

    两个软件的作用:1.samtools mpileup 主要是用于收集BAM文件中的信息,这个位点上有多少条read匹配,匹配read的碱基是什么,并将这些信息存储在BCF文件中.2.bcftools ...

  5. MySQL数据库基本操作(四)

    在进行查询之前,我们要先建好关系表,并往数据表中插入些数据.为查询操作做好准备. 五张关系表的创建: #创建并进入数据库: mysql> CREATE DATABASE `info`; Quer ...

  6. java基础之bit、byte、char、String

    bit 位,二进制数据0或1 byte 字节,一个字节等于8位二进制数 char 字符, String 字符串,一串字符 常见转换 1 字母  = 1byte = 8 bit 1 汉字  = 2byt ...

  7. MIPI DBI\DPI\DSI简介【转】

    本文转载自:http://blog.csdn.net/longxiaowu/article/details/24249971 (1)DBI接口 A,也就是通常所讲的MCU借口,俗称80 system接 ...

  8. Mysql中in语句排序

    这只是Mysql语句的写法,不同数据库写法不太一样, ,,,) order by instr('1,11115,11140,11135',id); 如果不使用order by,in语句查询出来的顺序是 ...

  9. Luogu-3966 [TJOI2013]单词

    这道题应该是后缀数组的套路题啊,把单词连接起来,中间用没有出现过且互不相同的字符来分隔开,求一下\(height\)数组. 对于一个单词来说,设单词长\(len\),所在的后缀为\(i\),如果某后缀 ...

  10. ubuntu12.04 错误:phonon: No such file or directory; phonon模块的安装

    http://blog.csdn.net/xsl1990/article/details/9009919 错误:phonon: No such file or directory 1)sudo  ap ...