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 ;
}
CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)的更多相关文章
- Codeforces 803F Coprime Subsequences (容斥)
Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[ ...
- Codeforces 803F - Coprime Subsequences(数论)
原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...
- CodeForces 803F Coprime Subsequences
$dp$. 记$dp[i]$表示$gcd$为$i$的倍数的子序列的方案数.然后倒着推一遍减去倍数的方案数就可以得到想要的答案了. #include <iostream> #include ...
- [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]
题面 传送门 思路 新姿势get 莫比乌斯容斥 $\sum_{i=1}{n}\mu(i)f(i)$ 这个东西可以把所有没有平方质因子的东西表示出来,还能容斥掉重复的项 证明是根据莫比乌斯函数的定义,显 ...
- CF(439E - Devu and Birthday Celebration)莫比乌斯容斥
题意:将n个糖果插入f-1个挡板分成f分(a1,a2,a3...af). 问有多少种分法能够使得gcd(a1,a2,a3...af)=1; 解法.莫比乌斯容斥,首先按1为单位分,这时候有C(n-1,f ...
- Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)
大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其 ...
- HihoCoder - 1867: GCD (莫比乌斯容斥)
Sample Input 6 1 6 2 5 3 4 Sample Output 10 You are given a {1, 2, ..., n}-permutation a[1], a[2], . ...
- HDU 4135 Co-prime 欧拉+容斥定理
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Codeforces Round #258 (Div. 2) 容斥+Lucas
题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...
随机推荐
- 01 Spring框架 基本介绍
相信学习java,并且走Web道路的道友都应该知道Spring的大名,它的地位相信也不需要我在这里多说什么,接下来的文章就Spring的配置和使用来进行一些讲解. 首先学习框架我们都要考虑和做到以下几 ...
- 缓存:Memcached Redis
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- flex label 换行
Flex中label换行有两种情况 在AS中赋值: label.text="Online\r\nResources" 在mxml中赋值: text="Online Res ...
- Listening Carefully SP1403
Listening Carefully仔细聆听When people talk, listen completely. Most people never listen. ―Ernest Heming ...
- scope的继承
本文转载自: http://www.tuicool.com/articles/63iEref angular中scope的继承与js的原型继承有密切的联系,首先回顾一下js的继承: function ...
- python脚本发送电子邮件
#!/usr/bin/pythonimport smtplibimport stringHOST='smtp.qq.com'#HOST='mail.qq.com'SUBJECT='Test email ...
- Luogu-3346 [ZJOI2015]诸神眷顾的幻想乡
\(trie\)树建广义后缀自动机: \(dfs\)遍历\(trie\)树,将树上的一个节点插入\(sam\)时,将他的\(fa\)在\(sam\)上所在的节点作为\(last\) #include& ...
- 解决maven寻找依赖关系失败的问题
在mac中会碰到依赖jdk自带的jar包而maven找不到的问题 解决方案:安装jdk中的tools到本地mvn库 mvn install:install-file -Dfile=${JAVA_HOM ...
- HDFS请求接口
通过HTTP来访问HDFS有两种方法:直接访问,HDFS后台进程直接服务于来自客户端的请求:通过代理(一对多)访问,客户端通常使用DistributedFileSystemAPI访问HDFS.第一种情 ...
- lua 写逻辑打印log时,打印到一半后停止不再打印,程序停止
问题描述:ubuntu下用lua开发游戏电子邮件模块,自己测试时向用户推送100封,而用户最多只能有50封.这是调用sysdelete删除一些邮件.当打印log时,打印到一半后程序中途停止.将打印lo ...