n<=500000个2<=Ai<=1e7的数,求这样选数的方案数:先从其中挑出一个gcd不为1的集合,然后再选一个不属于该集合,且与该集合内任意一个数互质的数。

好的统计题。

其实就是要对每个数求和他互质的,gcd不为1的集合数,容斥一下,求出所有gcd不为1的集合数A然后减去所有他的质因子对这个A的贡献。(这里的A是CF的题解的B)

那先看看所有gcd不为1的集合数怎么求。比如说2的倍数有cnt_2个,那能凑出2^cnt_2-1个集合,然后3的倍数有cnt_3个,能凑出2^cnt_3-1个集合,但有一些gcd为6的集合被算了两次,就要减去2^cnt_6-1,等等这不是莫比乌斯函数嘛,所以现在只要统计1~1e7中每个数作为多少个数的因数即可。那要把n个数都进行分解,这里可以在筛莫比乌斯的时候记一下每个数的最小质因子就可以n*logMax的时间内完成所有数的分解。由于miu_i=0的cnt_i对答案没贡献,所以每个数分解完的质因子不用去考虑那些次数大于1的部分,比如12=2*2*3直接看2和3即可。把不重复质数分解出来后,在1e7内一个数最多有8个不同质因子,所以枚举一下所有这些质因子能凑出的数即可计算miu_i不为0的cnt_i。

然后某个数的质因子对A的贡献呢?同理耶!

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
//#include<iostream>
using namespace std; int n;
#define maxn 500011
#define maxm 10000011
const int mod=1e9+;
int a[maxn]; int xiao[maxm],miu[maxm],prime[maxm],lp;bool notprime[maxm];
void pre(int n)
{
lp=;notprime[]=;
for (int i=;i<=n;i++)
{
if (!notprime[i]) {prime[++lp]=i;miu[i]=-;}
for (int j=;j<=lp && 1ll*prime[j]*i<=n;j++)
{
notprime[prime[j]*i]=;
xiao[prime[j]*i]=prime[j];
if (!(i%prime[j])) {miu[i*prime[j]]=;break;}
else miu[i*prime[j]]=-miu[i];
}
}
} int cnt[maxm],two[maxn];
int frac[],lf;
int main()
{
scanf("%d",&n);int Max=;
for (int i=;i<=n;i++) scanf("%d",&a[i]),Max=max(Max,a[i]);
two[]=;for (int i=;i<=n;i++) two[i]=(two[i-]<<)%mod;
pre(Max);
for (int i=;i<=n;i++)
{
int tmp=a[i];lf=;
while (xiao[tmp])
{
int now=xiao[tmp];
while (xiao[tmp]==now) tmp/=xiao[tmp];
frac[++lf]=now;
}
if (!lf || frac[lf]!=tmp) frac[++lf]=tmp;
for (int i=;i<(<<lf);i++)
{
int now=;
for (int j=;j<=lf;j++) if (i&(<<(j-))) now*=frac[j];
cnt[now]++;
}
}
int A=;
for (int i=;i<=Max;i++) A=(A-miu[i]*(two[cnt[i]]-))%mod; int ans=;
for (int i=;i<=n;i++)
{
int tmp=a[i];lf=;
while (xiao[tmp])
{
int now=xiao[tmp];
while (xiao[tmp]==now) tmp/=xiao[tmp];
frac[++lf]=now;
}
if (!lf || frac[lf]!=tmp) frac[++lf]=tmp;
int B=;
for (int i=;i<(<<lf);i++)
{
int now=;
for (int j=;j<=lf;j++) if (i&(<<(j-))) now*=frac[j];
B=(B-miu[now]*(two[cnt[now]]-))%mod;
}
ans=((ans+A)%mod-B)%mod;
}
printf("%d\n",(ans+mod)%mod);
return ;
}

CF585E:Present for Vitalik the Philatelist的更多相关文章

  1. 「CF585E」 Present for Vitalik the Philatelist

    「CF585E」 Present for Vitalik the Philatelist 传送门 我们可以考虑枚举 \(S'=S\cup\{x\}\),那么显然有 \(\gcd\{S'\}=1\). ...

  2. 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\) ...

  3. 【CodeForces】585 E. Present for Vitalik the Philatelist

    [题目]E. Present for Vitalik the Philatelist [题意]给定n个数字,定义一种合法方案为选择一个数字Aa,选择另外一些数字Abi,令g=gcd(Ab1...Abx ...

  4. 【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 ...

  5. CF 585 E Present for Vitalik the Philatelist

    CF 585 E Present for Vitalik the Philatelist 我们假设 $ f(x) $ 表示与 $ x $ 互质的数的个数,$ s(x) $ 为 gcd 为 $ x $ ...

  6. Codeforces 585E. Present for Vitalik the Philatelist(容斥)

    好题!学习了好多 写法①: 先求出gcd不为1的集合的数量,显然我们可以从大到小枚举计算每种gcd的方案(其实也是容斥),或者可以直接枚举gcd然后容斥(比如最大值是6就用2^cnt[2]-1+3^c ...

  7. Codeforces 585E - Present for Vitalik the Philatelist(简单莫反+狄利克雷前缀和)

    Codeforces 题目传送门 & 洛谷题目传送门 一道不算太难的 D1E 罢--虽然我不会做/kk u1s1 似乎这场 Div1 挺水的?F 就是个 AC 自动机板子还被评到了 3k2-- ...

  8. 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\)是数组中和 ...

  9. CF585E-Present for Vitalik the Philatelist【莫比乌斯反演,狄利克雷前缀和】

    正题 题目链接:https://www.luogu.com.cn/problem/CF585E 题目大意 给出一个大小为\(n\)的可重集\(T\),求有多少个它的非空子集\(S\)和元素\(x\)满 ...

随机推荐

  1. 448 Find All Numbers Disappeared in an Array 找到所有数组中消失的数字

    给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字.您能在不使用 ...

  2. C. Two strings 二分 + 预处理

    http://codeforces.com/contest/762/problem/C 第一个串str[],第二个sub[] 预处理出prefix[i]表示sub的前i位和str[]的最长lcs去到s ...

  3. WindowForm.计算器

    设计计算器: 外部变量: 数字键按钮: 运算符按钮事件代码: 清零按钮 等号按钮: 思维导图:

  4. swiper4实现的拥有header和footer的全屏滚动demo/swiper fullpage footer

    用swiper4实现的拥有header和footer的全屏滚动demo, <!DOCTYPE html> <html lang="en"> <head ...

  5. 【转】Nicescroll滚动条插件的用法

    原网址:http://blog.csdn.net/mss359681091/article/details/52838179 Nicescroll滚动条插件是一个非常强大的基于JQUERY的滚动条插件 ...

  6. applicationContext.getBean(“loginEntity”)

    <!-- 指定Spring需要扫描的包,并将所有是别的类放到容器中,便于识别被注解的受托管bean --> <context:component-scan base-package= ...

  7. 未找到框架“.NETFramework,Version=v4.5”的引用程序集

    问题描述 一般是在编译的时候会出现这样子的问题, 问题原因: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETF ...

  8. close - 关闭一个文件描述符

    SYNOPSIS 总览 #include <unistd.h> int close(int fd); DESCRIPTION 描述 close 关闭 一个 文件 描述符 , 使它 不在 指 ...

  9. 关于 <script type='text/template' > 的妙用 / 使用jquery获取iframe加载完成事件

    https://www.cnblogs.com/ddqyc/p/6200539.html <!DOCTYPE html> <html> <head> <met ...

  10. 服务器中打开IIS管理器

    1.选远程连接服务器,然后开始>控制面板>打开或关闭Windows功能>服务器管理器>web服务器>internet信息服务的展开下一项即可,如图: