转化为质数域上的操作,如果用莫反的话,记录因数的cnt。

其实莫反的推式子最后和容斥做法殊途同归了,容斥的系数就是莫比乌斯函数。

const int maxn = 2e5 + 5, maxa = 5e5 + 5;

int n, q, a[maxn], maxx;
int primes[maxa], tot, vis[maxa], mu[maxa];
vector<int> fac[maxa]; ll ans;
int g[maxa];
bool mark[maxn]; void Pre() {
mu[1] = 1;
for (int i = 2; i <= maxx; i++) {
if (!vis[i]) {
primes[++tot] = i;
mu[i] = -1;
}
for (int j = 1; j <= tot && (ll)primes[j] * i <= maxx; j++) {
vis[primes[j] * i] = 1;
if (i % primes[j] == 0) break;
mu[primes[j] * i] = -mu[i];
}
}5
for (int i = 1; i <= maxx; i++)
for (int j = 1; (ll)j * i <= maxx; j++)
fac[j * i].push_back(i);
} int main() {
read(n), read(q);
rep(i, 1, n) read(a[i]), maxx = max(maxx, a[i]);
Pre();
for (int i; q; q--) {
read(i);
if (!mark[i]) {
mark[i] = 1;
for (int d : fac[a[i]]) {
ans += mu[d] * g[d];
g[d]++;
}
} else {
mark[i] = 0;
for (int d : fac[a[i]]) {
g[d]--;
ans -= mu[d] * g[d];
}
}
writeln(ans);
}
return 0;
}

Codeforces 548E(莫反、容斥)的更多相关文章

  1. Codeforces 100548F - Color (组合数+容斥)

    题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...

  2. Codeforces 803F Coprime Subsequences (容斥)

    Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[ ...

  3. Codeforces 920G(二分+容斥)

    题意: 定义F(x,p)表示的是一个数列{y},其中gcd(y,p)=1且y>x 给出x,p,k,求出F(x,p)的第k项 x,p,k<=10^6 分析: 很容易想到先二分,再做差 然后问 ...

  4. HDU 5213 Lucky 莫队+容斥

    Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...

  5. codeforces 571A--Lengthening Sticks(组合+容斥)

    A. Lengthening Sticks time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. 双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)

    题意:https://codeforc.es/contest/1207/problem/D n个元素,每个元素有a.b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时g ...

  7. Codeforces 548E Mike ans Foam (与质数相关的容斥多半会用到莫比乌斯函数)

    题面 链接:CF548E Description Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a sp ...

  8. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  9. hdu_5213_Lucky(莫队算法+容斥定理)

    题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...

随机推荐

  1. hdu 4704 sum(费马小定理+快速幂)

    题意: 这题意看了很久.. s(k)表示的是把n分成k个正整数的和,有多少种分法. 例如: n=4时, s(1)=1     4 s(2)=3     1,3      3,1       2,2 s ...

  2. super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

    super.onCreate(savedInstanceState) 调用父类的onCreate构造函数. 当一个Activity在生命周期结束前,会调用onSaveInsanceState()这个回 ...

  3. (Nginx + Gunicorn) 时 nginx 400 request line is too large (4360 4094)

    查看nginx下面两个参数 值是否满足 client_header_buffer_size 512k;large_client_header_buffers 4 512k; 满足依然出现 如果ngin ...

  4. 集训Day2

    雅礼集训2017Day2 T1 给你一个水箱,水箱里有n-1个挡板,水遵循物理定律 给你m个条件,表示第i个格子上面y+1高度的地方有或没有水 现在给你无限的水从任意地方往下倒,问最多满足多少条件 n ...

  5. java面试题06

    题目: 数据库 1. 表名:g_cardapply 字段(字段名/类型/长度): g_applyno varchar 8://申请单号(关键字) g_applydate bigint 8://申请日期 ...

  6. ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)

    Description Let's consider one interesting word game. In this game you should transform one word int ...

  7. 算法导论笔记——第十八章 B树

    18.1 B树的定义  18.2 B树的基本操作 与一棵二叉搜索树一样,可以在从树根到叶子这个单程向下过程中将一个新的关键字插入B树中.为了做到这一点,当沿着树向下查找新的关键字所属位置时,就分裂沿途 ...

  8. python list列表sort、sorted、reverse排序

    列表排序:sort是修改原列表,sorted提供原列表的一个有序副本 li=[2,1,4,5,0]li.sort() #默认从小到大print li结果:[0, 1, 2, 4, 5] li=[2,1 ...

  9. C# 中的迭代器 yield关键字 提高性能和可读性

    展示一个例子 IList<string> FindBobs(IEnumerable<string> names) { var bobs = new List<string ...

  10. php查询内存信息

    php查询内存信息,是为了更好的查看内存使用情况,更好的优化代码. 查看当前内存使用情况使用:memory_get_usage()函数. 查看内存使用峰值:memory_get_peak_usage( ...