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;
    }
  • 相关阅读:
    智能指针和二叉树(2):资源的自动管理
    c++智能指针和二叉树(1): 图解层序遍历和逐层打印二叉树
    QLineEdit拾遗:数据的过滤、验证和补全
    为Qt视图中的文字添加彩虹渐变效果
    python3的变量作用域规则和nonlocal关键字
    三种方法为QLineEdit添加清除内容按钮
    配置CLion作为Qt5开发环境
    c++随机排序容器中的元素
    c++性能测试工具:google benchmark入门(一)
    shared_ptr和动态数组
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9638601.html
Copyright © 2011-2022 走看看