题面

链接:CF548E

Description

Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered from 1 to n. i-th kind of beer has a**i milliliters of foam on it.

Maxim is Mike's boss. Today he told Mike to perform q queries. Initially the shelf is empty. In each request, Maxim gives him a number x. If beer number x is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf.

After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs (i, j) of glasses in the shelf such that $i< j $and where is the greatest common divisor of numbers \(a\) and \(b\).

Mike is tired. So he asked you to help him in performing these requests.

Input

The first line of input contains numbers \(n\) and \(q (1 ≤ n, q ≤ 2 × 10^5)\), the number of different kinds of beer and number of queries.

The next line contains n space separated integers, \(a_1, a_2, ... , a_n (1 ≤ a_i ≤ 5 × 10^5)\), the height of foam in top of each kind of beer.

The next q lines contain the queries. Each query consists of a single integer integer x(1 ≤ x ≤ n), the index of a beer that should be added or removed from the shelf.

Output

For each query, print the answer for that query in one line.

Examples


input output
5 6
1 2 3 4 6
1
2
3
4
5
1
0
1
3
5
6
2

题目分析

如题,用\(\mu\)进行容斥即可。

代码实现

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#define MAXN 0x7fffffff
typedef long long LL;
const int N=500005;
using namespace std;
inline int Getint(){register int x=0,f=1;register char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}return x*f;}
int prime[N],mu[N];
bool vis[N],chk[200005];
int a[200005],cnt[N]; int main(){
mu[1]=1;
for(int i=2;i<=5e5;i++){
if(!vis[i])prime[++prime[0]]=i,mu[i]=-1;
for(int j=1;j<=prime[0]&&i*prime[j]<=5e5;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0)break;
mu[i*prime[j]]=-mu[i];
}
} int n=Getint(),q=Getint();
for(int i=1;i<=n;i++)a[i]=Getint();
LL ans=0;
for(int j=1;j<=q;j++){
int x=Getint(),ret=0;
if(!chk[x]){
chk[x]=1,x=a[x];
for(int i=1,lim=sqrt(x);i<=lim;i++){
if(x%i==0){
ret+=mu[i]*cnt[i],cnt[i]++;
if(i*i!=x)ret+=mu[x/i]*cnt[x/i],cnt[x/i]++;
}
}
ans+=ret;
}else{
chk[x]=0,x=a[x];
for(int i=1,lim=sqrt(x);i<=lim;i++){
if(x%i==0){
cnt[i]--,ret+=mu[i]*cnt[i];
if(i*i!=x)cnt[x/i]--,ret+=mu[x/i]*cnt[x/i];
}
}
ans-=ret;
}
cout<<ans<<'\n';
}
return 0;
}
/*
8 8
77770 436260 150552 164101 453308 1 1 329637
1 2 3 4 5 6 7 8
*/

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

  1. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

  2. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

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

  4. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

  5. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  6. Codeforces Round #330 (Div. 2)B. Pasha and Phone 容斥

    B. Pasha and Phone   Pasha has recently bought a new phone jPager and started adding his friends' ph ...

  7. codeforces 547c// Mike and Foam// Codeforces Round #305(Div. 1)

    题意:给出数组arr和一个空数组dst.从arr中取出一个元素到dst为一次操作.问每次操作后dst数组中gcd等于1的组合数.由于数据都小于10^6,先将10^6以下的数分解质因数.具体来说从2开始 ...

  8. Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)

    题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...

  9. Codeforces Round #428 (Div. 2) D. Winter is here 容斥

    D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...

随机推荐

  1. 夏令营501-511NOIP训练18——高二学堂

    传送门:QAQQAQ 题意:给你一个数$n$,把它拆分成至多$k$个正整数,使得这些数的和等于$n$且每一个正整数的个数不能超过$4$ 拆分的顺序是无序的,但取出每一个数方案是不同的(例如我要拆$1$ ...

  2. Git及github使用(三)更新自己的github代码

    如果之前上传的代码到目前有所改动,想要更新github上的代码文件.希望本篇对你有所帮助. 1.拉取代码本地修改后上传代码 提交成功后的效果如下: 2.更新展示在github首页的readme内容 上 ...

  3. spark入门到精通(后续开始学习)

    早几年国内外研究者和业界比较关注的是在 Hadoop 平台上的并行化算法设计.然而, HadoopMapReduce 平台由于网络和磁盘读写开销大,难以高效地实现需要大量迭代计算的机器学习并行化算法. ...

  4. 阿里数据库大牛的 MySQL 学习指南!

    做后端的同学,总是绕不开MySQL. 毫无疑问,MySQL 是当下最流行的开源数据库.凭借强大的性能和易于使用性,它已被Google.Facebook.YouTube.百度.网易和新浪等大型互联网公司 ...

  5. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  6. Datagrid 的 SelectItem 和 SelectValue 如何区分、DataContext 和 ItemSource 在绑定时该绑哪个?

    1.selecteditem.selectedvalue.selectedvaluepath三个属性 场景: class T { public string A { get; set; } publi ...

  7. codis 使用

    1:Jedis与Redisson对比 2.1. 概况对比 Jedis是Redis的Java实现的客户端,其API提供了比较全面的Redis命令的支持:Redisson实现了分布式和可扩展的Java数据 ...

  8. Django 前后端数据传输、ajax、分页器

    返回ORM目录 Django ORM 内容目录: 一.MTV与MVC模式 二.多对多表三种创建方式 三.前后端传输数据 四.Ajax ​ 五.批量插入数据 六.自定义分页器 一.MTV与MVC模式 M ...

  9. 理解Spring框架中Bean的5个作用域

    当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleton:单例模式,在整个Spring I ...

  10. VBA文件对话框的应用(VBA打开文件、VBA选择文件、VBA选择文件夹)

    在VBA中经常要用到文件对话框来进行打开文件.选择文件或选择文件夹的操作.用Microsoft Office提供的文件对话框比较方便.用法如下Application.FileDialog(fileDi ...