「CF585E」 Present for Vitalik the Philatelist
「CF585E」 Present for Vitalik the Philatelist
我们可以考虑枚举 \(S'=S\cup\{x\}\),那么显然有 \(\gcd\{S'\}=1\)。
那么我们从里面可以选一个数出来作为 \(x\),共有 \(|S'|\) 种可能,我们记为 \((x,S)\)。
但是这样显然会计算到一些不合法的情况,考虑统计。
对于一个集合 \(S\),若其 \(\gcd\) 为 \(1\),则再任意添加一个数 \(\gcd\) 仍为 \(1\),这样的二元组显然是不合法的,共有 \(n-|S|\) 种可能。
所以对于一个 \(\gcd\) 为 \(1\) 的集合 \(S\),其对答案的贡献就是 \(|S|-(n-|S|)=2|S|-n\)。
设给出的数的集合为 \(U\),故答案可表示为
\]
显然可以考虑莫比乌斯反演,于是有
\]
令 \(T=U\cap\{x|x=kd,k\in \mathbb{Z}\}\),我们的问题就变为了计算
\]
首先这个不等于空集很烦,我们可以把它加上最后再减去,变为
\]
后面那部分很好算,共有 \(2^{|T|}\) 种集合,答案即为 \(2^{|T|}n\)。
前一部分可以考虑计算每个数对答案的贡献,枚举选某个数(共 \(|T|\) 种可能),剩下的 \(|T|-1\) 个元素都是选或不选,故答案为 \(2(2^{|T|-1}|T|)=2^{|T|}|T|\)。
然后问题就变成了如何求 \(T\)。
这个东西可以直接分解质因数,也可以用 \(\text{Dirichlet}\) 后缀和,在此不再赘述。
总复杂度大概是 \(O(V\log_2\log_2V+n)\) 的,其中 \(V\) 表示值域。
贴代码
/*---Author:HenryHuang---*/
/*---Never Settle---*/
/*---Never Enough---*/
#include<bits/stdc++.h>
#define module(x) ((x)>=p?((x)-p):(x))
using namespace std;
const int maxn=1e7+5;
const int p=1e9+7;
int pri[maxn],pp[maxn],mu[maxn],cnt;
int p2[maxn];
int sum[maxn];
void init(int mx){
mu[1]=1;
for(int i=2;i<=mx;++i){
if(!pp[i]) pri[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*pri[j]<=mx;++j){
pp[i*pri[j]]=1;
if(i%pri[j]==0){
mu[i*pri[j]]=0;
break;
}
else mu[i*pri[j]]=-mu[i];
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n;cin>>n;
p2[0]=1;
int mx=0;
for(int i=1;i<=n;++i){
int x;cin>>x;mx=max(mx,x);
++sum[x];
p2[i]=module(p2[i-1]<<1);
}
init(mx);
for(int i=1;i<=cnt;++i)
for(int j=mx/pri[i];j;--j)
sum[j]+=sum[j*pri[i]];
int ans=0;
for(int i=1;i<=mx;++i){
if(mu[i]){
int tmp=1ll*(1ll*p2[sum[i]]*(sum[i]-n+p)+n)%p;
if(mu[i]>0) ans=module(ans+tmp);
else ans=module(ans-tmp+p);
}
}
cout<<module(ans+p)<<'\n';
return 0;
}
「CF585E」 Present for Vitalik the Philatelist的更多相关文章
- CF585E:Present for Vitalik the Philatelist
n<=500000个2<=Ai<=1e7的数,求这样选数的方案数:先从其中挑出一个gcd不为1的集合,然后再选一个不属于该集合,且与该集合内任意一个数互质的数. 好的统计题. 其实就 ...
- CF585E. Present for Vitalik the Philatelist [容斥原理 !]
CF585E. Present for Vitalik the Philatelist 题意:\(n \le 5*10^5\) 数列 \(2 \le a_i \le 10^7\),对于每个数\(a\) ...
- 【CF 585E】 E. Present for Vitalik the Philatelist
E. Present for Vitalik the Philatelist time limit per test 5 seconds memory limit per test 256 megab ...
- 【CodeForces】585 E. Present for Vitalik the Philatelist
[题目]E. Present for Vitalik the Philatelist [题意]给定n个数字,定义一种合法方案为选择一个数字Aa,选择另外一些数字Abi,令g=gcd(Ab1...Abx ...
- CF 585 E Present for Vitalik the Philatelist
CF 585 E Present for Vitalik the Philatelist 我们假设 $ f(x) $ 表示与 $ x $ 互质的数的个数,$ s(x) $ 为 gcd 为 $ x $ ...
- Codeforces 585E. Present for Vitalik the Philatelist(容斥)
好题!学习了好多 写法①: 先求出gcd不为1的集合的数量,显然我们可以从大到小枚举计算每种gcd的方案(其实也是容斥),或者可以直接枚举gcd然后容斥(比如最大值是6就用2^cnt[2]-1+3^c ...
- E. Present for Vitalik the Philatelist 反演+容斥
题意:给n个数\(a_i\),求选一个数x和一个集合S不重合,gcd(S)!=1,gcd(S,x)==1的方案数. 题解:\(ans=\sum_{i=2}^nf_ig_i\),\(f_i\)是数组中和 ...
- Codeforces 585E - Present for Vitalik the Philatelist(简单莫反+狄利克雷前缀和)
Codeforces 题目传送门 & 洛谷题目传送门 一道不算太难的 D1E 罢--虽然我不会做/kk u1s1 似乎这场 Div1 挺水的?F 就是个 AC 自动机板子还被评到了 3k2-- ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
随机推荐
- Go语言web开发---Beego路由
参考链接(查看更多):https://beego.me/docs/mvc/controller/router.md 基础路由 从 beego 1.2 版本开始支持了基本的 RESTful 函数式路由, ...
- pytest + allure
参考allure官网: https://docs.qameta.io/allure/#_pytest 1.使用命令pip install pytest-allure安装 注意:我遇到的一个现象,用命令 ...
- 改造 Firefox 浏览器——GitHub 热点速览
作者:HelloGitHub-小鱼干 上周推荐了一个可以在浏览器上用 VS Code 的项目,这次 Firefox-UI-Fix 带你给 Firefox 来个大变身,在它现有 Proton UI 下进 ...
- HarmonyOS技术特性
HarmonyOS技术特性 硬件互助,资源共享 多种设备之间能够实现硬件互助.资源共享,依赖的关键技术包括分布式软总线.分布式设备虚拟化.分布式数据管理.分布式任务调度等. 分布式软总线 分布式软总线 ...
- python_reques接口测试框架,Excel作为案例数据源
一.框架菜单 1.1 common模块 1.2 其他 二.Excel接口测试案例编写 三.读取Excel测试封装(核心封装) excel_utils.py 读取Excel中的数据 import o ...
- Kubernetes 实战——升级应用(Deployment)
一.更新运行在 Pod 内的应用程序 1. 修改 Pod 模板 将导致应用程序在一定时间内不可用 2. 修改 Service 的 Pod 选择器 需要同时运行两倍的 Pod 3. 滚动升级 应用程序需 ...
- 【CMD】修改文件夹的属性
将 d:\文件夹 添加 只读 属性: attrib +r d:\文件夹 /d 将 d:\文件夹 去掉 只读 属性: attrib -r d:\文件夹 /d 其他属性可以参考帮助文件:
- 使用sign签名发送请求
import CryptoJS from "crypto-js"; import urlencode from "urlencode"; methods:{ a ...
- 「10.12」木板(数学)·打扫卫生(神仙DP)
A. 木板 一个很简单的数学题,简单推一下就好,路丽姐姐教你学数学. 将式子化出我们发现只需求出$i\times i/n$的个数 那么我们将$n$质因数分解,可知因子个数 为了整除$n$,令$i==\ ...
- 手把手教你IDEA连接码云(Gitee)
目录 前言 一.下载.安装git 1.打开git官网,选择你的操作系统 2.根据你的系统位数选择相应的版本下载 3.安装 4.配置全局的用户名.邮箱 5.在idea中配置git目录 二.配置Gitee ...