题意:给n个数\(a_i\),求选一个数x和一个集合S不重合,gcd(S)!=1,gcd(S,x)==1的方案数.

题解:\(ans=\sum_{i=2}^nf_ig_i\),\(f_i\)是数组中和i的gcd不为1的个数,\(g_i\)是选取集合gcd为i的方案数.

\(f_n=\sum_{i=1}^N[gcd(n,i)!=1]a_i\)

\(f_n=\sum_{i=1}^N\sum_{d|gcd(i,n)}\mu(d)a_i\)

\(f_n=\sum_{d|n}\mu(d)\sum_{i=1}^{\frac{N}{d}}a_{i*d}\)

\(f\)可以\(nlogn\)预处理

\(g_i=2^{b_i}-1-\sum_{i|x}g(x)\),\(b_i\)是数组中是i倍数的数的个数,可以\(nlogn\)处理

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std;
//using namespace __gnu_pbds; const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=10000000+10,maxn=500000+10,inf=0x3f3f3f3f; struct FastIO {
static const int S = 1e7;
int wpos;
char wbuf[S];
FastIO() : wpos(0) {}
inline int xchar() {
static char buf[S];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, S, stdin);
if (pos == len) exit(0);
return buf[pos++];
}
inline int xuint() {
int c = xchar(), x = 0;
while (c <= 32) c = xchar();
for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x;
}
inline int xint()
{
int s = 1, c = xchar(), x = 0;
while (c <= 32) c = xchar();
if (c == '-') s = -1, c = xchar();
for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x * s;
}
inline void xstring(char *s)
{
int c = xchar();
while (c <= 32) c = xchar();
for (; c > 32; c = xchar()) * s++ = c;
*s = 0;
}
inline void wchar(int x)
{
if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
wbuf[wpos++] = x;
}
inline void wint(ll x)
{
if (x < 0) wchar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n) s[n++] = '0' + x % 10, x /= 10;
while (n--) wchar(s[n]);
wchar('\n');
}
inline void wstring(const char *s)
{
while (*s) wchar(*s++);
}
~FastIO()
{
if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
}
} io;
int mi[maxn],b[N],c[N],a[N],mu[N];
int main()
{
int n=io.xint(),ma=0;
mi[0]=1;
for(int i=1;i<=n;i++)
{
int x=io.xint();
b[x]++;ma=max(ma,x);
mi[i]=(1ll*mi[i-1]<<1)%mod;
}
mu[1]=1;
for(int i=1;i<=ma;i++)
{
for(int j=i<<1;j<=ma;j+=i)
b[i]+=b[j],mu[j]-=mu[i];
for(int j=i;j<=ma;j+=i)
c[j]+=mu[i]*b[i];
}
for(int i=ma;i;i--)
{
a[i]+=mi[b[i]]-1+mod;
if(a[i]>=mod)a[i]-=mod;
for(int j=i<<1;j<=ma;j+=i)
{
a[i]-=a[j];
if(a[i]<0)a[i]+=mod;
}
}
ll ans=0;
for(int i=2;i<=ma;i++)if(c[i]&&a[i])add(ans,1ll*c[i]*a[i]%mod);
io.wint(ans);
return 0;
}
/********************
1
10000000
********************/

E. Present for Vitalik the Philatelist 反演+容斥的更多相关文章

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

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

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

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

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

  5. 「CF585E」 Present for Vitalik the Philatelist

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

  6. CF 585 E Present for Vitalik the Philatelist

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

  7. CF585E:Present for Vitalik the Philatelist

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

  8. BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)

    [Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...

  9. BZOJ 2301 Problem b (莫比乌斯反演+容斥)

    这道题和 HDU-1695不同的是,a,c不一定是1了.还是莫比乌斯的套路,加上容斥求结果. 设\(F(n,m,k)\)为满足\(gcd(i,j)=k(1\leq i\leq n,1\leq j\le ...

随机推荐

  1. c++-字符串和时间操作

    C++ 字符串 C++ 提供了以下两种类型的字符串表示形式: C 风格字符串 C++ 引入的 string 类类型 C 风格字符串 C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持.字符 ...

  2. 使用Devstack部署neutron网络节点

    本文为minxihou的翻译文章,转载请注明出处Bob Hou: http://blog.csdn.net/minxihou JmilkFan:minxihou的技术博文方向是 算法&Open ...

  3. [转] .htaccess实现www 与没有www之间的重定向

    建站过程中有时候我们需要做这些设置 1.访问www 直接重定向到没有www上或者反过来,那么怎么通过.htaccess文件来实现呢. 1.首先服务器要支持Rewrite重写 2.创建.htaccess ...

  4. 13. DMA

    1. DMA简介 直接存储器存取(Dma)是为了提供高速数据传输外围设备和内存以及内存到内存.数据可以通过dma快速移动.没有任何CPU操作.这使得CPU资源可以用于其他操作. 这两个DMA控制器总共 ...

  5. mobile开发技巧

    1.隐藏地址栏 很多文档介绍通过调用 window.scrollTo(0, 1); 就可以隐藏地址栏,但是通过实践发现隐藏地址栏还是真够坑爹的啊,只调用这一句话一般不会起作用,我们需要 functio ...

  6. xml初步,DTD和Schema约束

    XML 可扩展的标记语言(!!!可扩展) 作用 1.存放数据 2.配置文件 语法 文档声明 <?xml version="1.0" encoding="UTF-8& ...

  7. nutch二次开发环境搭建

    开发环境: ubuntu14.04 + jdk1.7 + eclispe +nutch1.7 1:解压下好nutch1.7 src 源码(wget http://archive.apache.org/ ...

  8. axios全局拦截响应

    在系统开发过程中,若遇到长时间未操作,则需要将页面跳转到登录页面.因为现在都是前后端分离的开发模式,路由跳转都交给前端,而后端只返回一个报错信息,例如"errorMsg":&quo ...

  9. Mybatis使用Mapper方式CURD

    Mybatis 使用Dao代码方式进行增.删.改.查和分页查询. 1.Maven的pom.xml 2.配置文件 2.1.db.properties 2.2.mybatis.xml <?xml v ...

  10. iOS开发系列-异常处理

    概述 在开发中经常调用苹果的API遇到数组越界.实例方法不存在运行时等致命错误,此时程序直接奔溃.其实苹果是在函数内部抛出了一个异常.这样告诉开发者需要检查代码做修改.同样在我们自己封装一些框架或者功 ...