F. Coprime Subsequences
题目链接:
2 seconds
256 megabytes
standard input
standard output
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].
The first line contains one integer number n (1 ≤ n ≤ 100000).
The second line contains n integer numbers a1, a2... an (1 ≤ ai ≤ 100000).
Print the number of coprime subsequences of a modulo 109 + 7.
3
1 2 3
5
4
1 1 1 1
15
7
1 3 5 15 3 105 35
100 题意:给一个序列,问gcd为1的子序列有多少个; 思路:容斥,可以求出gcd为1的倍数的子序列个数,然后减去gcd为2,3,5,等等一个素数倍数的子序列个数再加上两个素数积倍数的子序列个数以此类推,
(注意容斥里面集合的交集里面不可能有4,9,12这种数,因为质因数分解后里面同一个质因数的个数>1,这就不可能在两个集合的交集里面); AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
const int mod=1e9+7;
int n,a[maxn],p[maxn];
int vis[maxn],num[maxn];
void init()
{
p[0]=1;
for(int i=1;i<maxn;i++){p[i]=p[i-1]*2;if(p[i]>=mod)p[i]-=mod;}
for(int i=2;i<maxn;i++)
{
if(!vis[i])
{
num[i]++;
for(int j=2*i;j<maxn;j+=i)
{
vis[j]=1;
if(num[j]==-1)continue;
int tep=j,x=0;
while(tep%i==0)x++,tep/=i;
if(x>1)num[j]=-1;
else num[j]++;
}
}
}
}
int main()
{
init();
scanf("%d",&n);
int x;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
for(int j=1;j*j<=x;j++)
{
if(x%j)continue;
a[j]++;
if(j*j!=x)a[x/j]++;
}
}
int ans=0;
for(int i=1;i<maxn;i++)
{
if(num[i]==-1)continue;
if(num[i]&1)ans=(ans-p[a[i]]+1);
else ans=(ans+p[a[i]]-1);
if(ans>=mod)ans-=mod;
else if(ans<0)ans+=mod;
}
printf("%d\n",ans);
return 0;
}
F. Coprime Subsequences的更多相关文章
- F. Coprime Subsequences 莫比乌斯反演
http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...
- CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)
Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...
- 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 [题意] 给你一个序列; 问你这个序列里面有多少个子列; 且这个子列里面的所有数字互质; [题解] ...
- 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 ...
- Educational Codeforces Round 20
Educational Codeforces Round 20 A. Maximal Binary Matrix 直接从上到下从左到右填,注意只剩一个要填的位置的情况 view code //#pr ...
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...
- 山东省第四届ACM省赛
排名:http://acm.sdut.edu.cn/sd2012/2013.htm 解题报告:http://www.tuicool.com/articles/FnEZJb A.Rescue The P ...
随机推荐
- List和Set区别
1. Set 接口实例存储的是无序的,不重复的数据.List 接口实例存储的是有序的,可以重复的元素. 2. Set检索效率低下,删除和插入效率高,插入和删除不会引起元素位置改变 <实现类有Ha ...
- 正则表达式和python的re模块
0 正则表达式 0.1 常见的元字符 .: 匹配除\r\n之外的任何单个字符 *: 匹配前面的子表达式任意次,例如Zz*可以匹配Z,可以匹配Zz,也可以匹配Zzzzzzzzzz +: ...
- LigerUI v1.2.4 LigerGrid默认样式 工具条背景白色
修改Aqua的ligerui-grid.css .l-panel-topbar 样式 修改为: .l-panel-topbar{padding: 0;background: #CEDFEF url(' ...
- Servlet+MyBatis项目转Spring Cloud微服务,多数据源配置修改建议
一.项目需求 在开发过程中,由于技术的不断迭代,为了提高开发效率,需要对原有项目的架构做出相应的调整. 二.存在的问题 为了不影响项目进度,架构调整初期只是把项目做了简单的maven管理,引入spri ...
- JSR规范整理
Web Service技术 Java Date与Time API ( JSR 310) Java API for RESTful Web Services (JAX-RS) 1.1 (JSR 311) ...
- 正则表达式【TLCL】
grep[global regular expression print] print lines matching a pattern grep [options] regex [file...] ...
- Linux系统官网下载
CentOS-6.9-x86_64-bin-DVD1.isohttp://archive.kernel.org/centos-vault/6.9/isos/x86_64/CentOS-6.9-x86_ ...
- Android中获取手机电量信息
有些时候我们需要在我们的应用上为用户展示当前手机的电量,这时候我们就需要用到广播了,我们都知道在动态注册广播的时候,我们需要传入一个BroadcastReceiver类对象,还有一个意图过滤器Inte ...
- 《Computational Statistics with Matlab》硬译
第1章 从随机变量采样 研究者提出的概率模型对于分析方法来说通常比较复杂,研究者处理复杂概率模型时越来越依赖计算.数值方法,通过使用计算方法,研究者就不用对一些分析技术做一些不现实的假设(如正态性和独 ...
- Pandas描述性统计
有很多方法用来集体计算DataFrame的描述性统计信息和其他相关操作. 其中大多数是sum(),mean()等聚合函数,但其中一些,如sumsum(),产生一个相同大小的对象. 一般来说,这些方法采 ...