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. vimium的使用介绍和基本用法

    vimium是chrome浏览器的一个插件,fq去chrome应用商店搜索vimium,下载安装 纯键盘操作,脱离了鼠标,提高效率 核心是f,安装好vimium后只需要按f,输入对应的编号就能进入相应 ...

  2. (C#)ListView双击Item事件

    /// <summary> /// 双击选择播放列表项进行播放 /// </summary> /// <param name="sender"> ...

  3. LeetCode:旋转链表【61】

    LeetCode:旋转链表[61] 题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5- ...

  4. input propertychange(1)

    input type=“text” 通过js改变输入框的value值是不会出发input propertychange事件

  5. PHP生成缩略图,控制图片质量,支持.png .jpg .gif

    namespace common\components; class ResizeImageHelper { public $type;//图片类型 public $width;//实际宽度 publ ...

  6. Docker容器技术-基础与架构

    一.什么是容器 容器是对应用程序及其依赖关系的封装. 1.容器的优点 容器与主机的操作系统共享资源,提高了效率,性能损耗低 容器具有可移植性 容器是轻量的,可同时运行数十个容器,模拟分布式系统 不必花 ...

  7. shell的符号总结

    1.命令替换符:先执行符号内的命令 反引号``:旧格式 $():新格式 2.字符串界定符: 单引号:保持引号内 的字符的字面值. 双引号:有些情况特殊. $echo '`date`' #不会执行`da ...

  8. 树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  9. freopen重定向输入

    #include <bits\stdc++.h> using namespace std; int main() { freopen("C:\\Users\\dcf\\Deskt ...

  10. SG函数略解

    由于笔者太懒,懒得把原来的markdown改成MCE,所以有很多奇怪的地方请谅解. 先说nim游戏. 大意:有n堆石子,两个人轮流取,每个人每次从任意一堆取任意个,直到一个人无法取了为止.问对于石子的 ...