zoukankan      html  css  js  c++  java
  • CodeForces

    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

    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

    题意:给定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;
    }
  • 相关阅读:
    excel批量导入后 数据库校验存储过程
    编译内核时覆盖KBUILD_BUILD_USER和KBUILD_BUILD_HOST
    EDID真实数据块,请参考标准文档仔细核对
    RK30SDK开发板驱动分析(二):DDR频率配置
    Linux中的固件加载例子
    Android中如何禁止音量调节至静音
    C++中内部类访问外部类的私有成员
    DSD, DFF, DSF, DST概念解析
    Android中播放DSD音乐
    Linux系统调用分析
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9638601.html
Copyright © 2011-2022 走看看