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
3
1 2 3
5
4
1 1 1 1
15
7
1 3 5 15 3 105 35
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=100010; const int Mod=1e9+7; int num[maxn],p[maxn],vis[maxn],mu[maxn],cnt,ans; vector<int>G[maxn]; int qpow(int a,int x){ int res=1; while(x){ if(x&1) res=(ll)res*a%Mod; x>>=1; a=(ll)a*a%Mod; } return res; } void prime() { mu[1]=1; rep(i,1,maxn-1) G[i].push_back(1); for(int i=2;i<maxn;i++){ if(!vis[i]) p[++cnt]=i,mu[i]=-1; for(int j=i;j<maxn;j+=i) G[j].push_back(i); for(int j=1;j<=cnt&&p[j]*i<maxn;j++){ mu[i*p[j]]=-mu[i]; vis[i*p[j]]=1; if(i%p[j]==0){mu[i*p[j]]=0; break;} } } } int main() { prime() ;int N,x; scanf("%d",&N); rep(i,1,N){ scanf("%d",&x); rep2(j,0,G[x].size()) num[G[x][j]]++; } rep(i,1,100000) ans=((ans+mu[i]*(qpow(2,num[i])-1))%Mod+Mod)%Mod; printf("%d ",ans); return 0; }