CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)
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
3
1 2 3
5
4
1 1 1 1
15
7
1 3 5 15 3 105 35
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=;
const int Mod=1e9+;
int num[maxn],p[maxn],vis[maxn],mu[maxn],cnt,ans;
vector<int>G[maxn];
int qpow(int a,int x){
int res=; while(x){
if(x&) res=(ll)res*a%Mod;
x>>=; a=(ll)a*a%Mod;
} return res;
}
void prime()
{
mu[]=; rep(i,,maxn-) G[i].push_back();
for(int i=;i<maxn;i++){
if(!vis[i]) p[++cnt]=i,mu[i]=-;
for(int j=i;j<maxn;j+=i) G[j].push_back(i);
for(int j=;j<=cnt&&p[j]*i<maxn;j++){
mu[i*p[j]]=-mu[i]; vis[i*p[j]]=;
if(i%p[j]==){mu[i*p[j]]=; break;}
}
}
}
int main()
{
prime() ;int N,x;
scanf("%d",&N);
rep(i,,N){
scanf("%d",&x);
rep2(j,,G[x].size()) num[G[x][j]]++;
}
rep(i,,) ans=((ans+mu[i]*(qpow(,num[i])-))%Mod+Mod)%Mod;
printf("%d\n",ans);
return ;
}
最新文章
- python函数
- spring+redis
- randomAccess接口
- 我所理解的readonly和const
- nginx 的动静分离配置(tomcat)
- 调用天气预报webservice
- ubuntu12.04+kafka2.9.2+zookeeper3.4.5的伪分布式集群安装和demo(java api)测试
- java方法可变参数的写法
- tarjan 边双连通分量 对点进行分组 每组点都在一个双连通分量里边
- [转] git reset简介
- 迭代器(iterators)
- Objects
- chrome添加扩展程序
- python 有参装饰器与迭代器
- Android Service用法知识点的讲解
- HOG feature
- Hive SQL 编译过程
- Hadoop高级培训课程大纲-开发者版
- 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)
- rootkit后门之安装流程
热门文章
- python之网络socket编程
- Android:日常学习笔记(8)———探究UI开发(3)
- Array排序方法sort()中的大坑
- 测试连接oracle数据库耗时
- linux中获取堆栈空间大小的方法
- cocos2dx打飞机项目笔记四:Enemy类和EnemyLayer类
- CentOS 7防火墙设置开放80端口
- JSP的动态Include的静态Include
- java深入探究11-基础加强
- Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named &#39;company&#39; in &#39;class java.lang.String&#39;