Codeforces 548E(莫反、容斥)
转化为质数域上的操作,如果用莫反的话,记录因数的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(莫反、容斥)的更多相关文章
- Codeforces 100548F - Color (组合数+容斥)
题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...
- Codeforces 803F Coprime Subsequences (容斥)
Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[ ...
- Codeforces 920G(二分+容斥)
题意: 定义F(x,p)表示的是一个数列{y},其中gcd(y,p)=1且y>x 给出x,p,k,求出F(x,p)的第k项 x,p,k<=10^6 分析: 很容易想到先二分,再做差 然后问 ...
- HDU 5213 Lucky 莫队+容斥
Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...
- codeforces 571A--Lengthening Sticks(组合+容斥)
A. Lengthening Sticks time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 双元素非递增(容斥)--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 ...
- 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 ...
- 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 ...
- hdu_5213_Lucky(莫队算法+容斥定理)
题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...
随机推荐
- JAVA-关键字&标识符
关键字: 关键字就是在java程序中具备特殊含义的标识符.关键字一般用于描述一个程序的结构或者表示数据类型.他们用来表示一种数据类型,或者表示程序的结构等,关键字不能用作变量名.方法名.类名.包名. ...
- BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...
- RQNOJ 329 刘翔!加油!:01背包
题目链接:https://www.rqnoj.cn/problem/329 题意: 刘翔有n封信,每封信都有自己的欣赏价值value[i].消耗时间time[i].消耗体力h[i].和得到的鼓舞w[i ...
- html5--1.4元素的属性
html5--1.4元素的属性 学习要点: 1.了解HTML元素属性2.学习两个属性:align和bgcolor 属性的作用就是就为元素提供更多的信息,大多数元素都可以拥有属性 属性的语法:<标 ...
- 如何设置android studio让程序运行在真机中
1.Run——>Edit Configurations... 2.运行
- 淘宝双十一页面(Flexible)
以下demo点我下载 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 百度地图API的第一次接触——热区
1.代码很简单 var map = new BMap.Map("container"); var point = new BMap.Point(116.404, 39.915); ...
- Oracle中的float类型字段
Oracle中的float类型对应着C#中的decimal类型
- XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译
XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译 <?xml version="1.0" encoding="UTF-8" ...
- POJ-3069
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10994 Accepted: 5555 D ...