莫比乌斯+容斥 cf803F

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
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
定义 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值