莫比乌斯+容斥 cf803F

来源:互联网 发布:带网络功能的winpe 编辑:程序博客网 时间:2024/06/05 04:16
F. Coprime Subsequences
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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].

Input

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

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

Output

Print the number of coprime subsequences ofa modulo109 + 7.

Examples
Input
31 2 3
Output
5
Input
41 1 1 1
Output
15
Input
71 3 5 15 3 105 35
Output
100
定义 f(i) 为以 igcd 的序列数,初始化f(i)=2c(i)1c(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;}


原创粉丝点击