Codeforces 803 F. prime subsequences (Mobius inversion + Combinatorics)

Description

In this paper, we give a sequence ai of length n, and ask how many subsequences of the sequence have gcd of 1

Input

In the first line, input an integer n to represent the sequence length, and then input n integers ai to represent the sequence (1 ≤ n,ai ≤ 105)

Output

gcd of the output a sequence is a subsequence of 1, and the result module is 109 + 7

Sample Input

3
1 2 3

Sample Output

5

Solution

F (d) is the number of gcd in a subsequence, and f (d) is the number of gcd divided by D in a subsequence

Count the number num(d) of the number divisible by D in a1,a2,...,an, then F(d)=2num(d) - - 1

Obviously, f (d) = ∑ d|nf(n), inversed by Mobius, f (d) = ∑ d|n μ (nd)F(n)

The required answer is f(1) = ∑ i=1m μ (i)F(i), where m=max(a1,a2,...,an)

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
#define mod 1000000007
int phi[maxn],mu[maxn],p[maxn],f[maxn],n,num[maxn];
void init(int n=1e5)
{
    mu[1]=1;
    int res=0;
    for(int i=2;i<=n;i++)
    {
        if(!phi[i])phi[i]=i-1,mu[i]=-1,p[res++]=i;
        for(int j=0;j<res&&i*p[j]<=n;j++)
        {
            if(i%p[j])
            {
                phi[i*p[j]]=(p[j]-1)*phi[i];
                mu[i*p[j]]=-mu[i];
            }
            else 
            {
                phi[i*p[j]]=p[j]*phi[i];
                mu[i*p[j]]=0;
                break;
            }
        }
    }
    f[0]=1;
    for(int i=1;i<=n;i++)f[i]=2*f[i-1]%mod;
} 
void add(int &x,int y)
{
    x=x+y>=mod?x+y-mod:x+y;
}
int main()
{
    init();
    scanf("%d",&n);
    int m=0;
    for(int i=1;i<=n;i++)
    {
        int temp;
        scanf("%d",&temp);
        m=max(m,temp);
        num[temp]++;
    }
    for(int i=1;i<=m;i++)
        for(int j=2*i;j<=m;j+=i)
            num[i]+=num[j];
    int ans=0;
    for(int i=1;i<=m;i++)
        if(mu[i]==1)add(ans,f[num[i]]-1);
        else if(mu[i]==-1)add(ans,mod-(f[num[i]]-1));   
    printf("%d\n",ans);
    return 0;
}

Posted by zipdisk on Sun, 03 May 2020 03:00:31 -0700