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 ...
随机推荐
- 序列化+protobuff+redis
背景: 当redis里面需要存储 “key-字符串,value-对象” 时,是不能直接存对象,而是需要将序列化后的对象存进redis. redis没有实现内部序列化对象的功能,所以需要自己提前序列化对 ...
- 每天一个Linux命令(40)vmstat命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控. 它能够对系统的整体情况进行统计,无法对某个进程 ...
- Mycat实现Mysql数据库读写分离
Linux和Windows环境下搭建Mycat数据读写分离 前提需要:1.服务器装有JVM虚拟机,就是JDK.2.两个Mysql数据库已经实现主从复制,参考:https://www.cnblogs.c ...
- bex5部署后不更新
哪个模块没更新,就编译哪个模块 在x5/tools/compile下,运行对应模块的bat,并清空浏览器缓存 如果修改了.w文件,也可以删除相应的.catch文件夹 和.release文件夹,并且注意 ...
- mysql case的语法
测试表:team 第一种语法: CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_ ...
- TIJ读书笔记02-控制执行流程
TIJ读书笔记02-控制执行流程 TIJ读书笔记02-控制执行流程 if-else 迭代 无条件分支 switch语句 所有条件语句都是以条件表达式的真假来决定执行路径,也就是通过布尔测试结果来决 ...
- gst-rtsp-server编译测试
最近在做dm368的开发,打算在368上移植个gst-rtsp-server.先在电脑上折腾了一天,终于是可以运行了. 我的虚拟机上早先已经安装了gstreamer-0.10(gstreamer版本太 ...
- UOJ132 【NOI2015】小园丁与老司机
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Debugging Tools for Windows__from WDK7
1. 主要要用到两个工具: (1).WinDBG 这个主要用于 非IDE下 调试程序/查看信息等 (2).cdb.exe 这个主要是用在 Qt5.3.2 for VS10 的单步调试器 2. WDK7 ...
- ubuntu安装与卸载.dep软件
一般情况下我们都是使用apt-get install进行软件安装,但是有时候也有可直接install的文件例如.deb. 接下来就记录一下.dep软件的安装与卸载 安装: 直接找到软件,双击就可以进行 ...