Let's call a non-empty sequence of positive integers a1, a2...akcoprime if the greatest common divisor of all elements of this sequence is equal to1.
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 are3 different subsequences:[1],[1] and [1, 1].
The first line contains one integer number n (1 ≤ n ≤ 100000).
The second line contains n integer numbersa1, a2...an (1 ≤ ai ≤ 100000).
Print the number of coprime subsequences ofa modulo109 + 7.
3 1 2 3
5
4 1 1 1 1
15
7 1 3 5 15 3 105 35
100
定义 f(i) 为以 i 为 gcd 的序列数,初始化 f(i)=2c(i)−1 , c(i) 是以 i 为因子的数的个数。
直接加起来肯定有重复的部分,所以从后往前筛 f(i)=f(i)−∑i|df(d) 即可。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
const double eps=1e-9;
ll bit[100010];
ll ans[100010];
int num[100010];
void init()
{
bit[0]=1;
for(int i=1;i<=100003;i++)
bit[i]=(bit[i-1]*2)%mod;
}
int main()
{
init();
int n,x,maxx=-1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
num[x]++;
maxx=max(maxx,x);
}
for(int i=maxx;i>0;i--)
{
int sum=0;
for(int j=i;j<=maxx;j+=i)
{
sum+=num[j];
ans[i]=(ans[i]-ans[j]+mod)%mod;
}
ans[i]=(ans[i]+bit[sum]-1)%mod;
}
printf("%lld\n",ans[1]);
return 0;
}