E. Mike and Foam


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
ai 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 × 105), the number of different kinds of beer and number of queries.

The next line contains n space separated integers,
a1, a2, ... , an (1 ≤ ai ≤ 5 × 105),
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.

Sample test(s)
Input
5 6
1 2 3 4 6
1
2
3
4
5
1
Output
0
1
3
5
6
2

题意简单易懂,我就不说了。。。

题解:题目能够转化为,在一个动态的集合中。没增加一个数。或取出一个数后,剩下的数中互质的有多少对。注意是无序的。 由于在题目的数据范围内质因子的数量非常少,复杂度基本上能够忽略不计,所以我们能够对每个新加进去的数。或者取出来的数进行质因子分解。然后通过容斥原理,先算出集合中有多少个与其不互质,这个非常easy算出来吧!

(假设想不出来能够看下我的代码,就是通过一个数组来计数,挺简单)。知道了不互质的。自然就知道互质了辣!

然后就答案减去或者加上互质的数就能够了。 时间复杂度预计10^7级别。

E. Mike and Foam(容斥原理)的更多相关文章

  1. E. Mike and Foam 容斥原理

    http://codeforces.com/problemset/problem/548/E 这题是询问id,如果这个id不在,就插入这个id,然后求a[id1] ,  a[id2]互质的对数. 询问 ...

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

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

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

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

  4. cf#305 Mike and Foam(容斥)

    C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. Codeforces548E:Mike and Foam

    Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n  ...

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

  7. Mike and Foam(位运算)

    English reading: bartender == barmaid:酒吧女招待 milliliter:毫升:千分之一毫升 foam:泡沫 a glass of beer with a good ...

  8. codeforces #305 C Mike and Foam

    首先我们注意到ai<=50w 因为2*3*5*7*11*13*17=510510 所以其最多含有6个质因子 我们将每个数的贡献分离, 添加就等于加上了跟这个数相关的互素对 删除就等于减去了跟这个 ...

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

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

随机推荐

  1. ADO.NET 2SqlDataAdapter、DataSet 的基本用法

    数据集完全独立于数据源,可以与数据源链接或者完全断开,其基本作用是为存储在内存缓存中的的数据提供关系视图 如果只是想读取和显示数据,则值需要使用数据读取器,尤其是处理大量数据的时候 如果需要处理数据, ...

  2. 如何捕获winform程序全局异常?(续)

    前言 上篇文章我提供了一种方案可以供我们捕获单线程程序中的所有未处理异常.但是如果程序是多线程,那么新增线程出现了异常上个方案就无能为力了.本着方案总比问题多的态度,我再给大家提供一种新的方案,供大家 ...

  3. 基于visual Studio2013解决C语言竞赛题之1050矩阵反斜线求和

       题目 解决代码及点评 /************************************************************************/ /* 50 ...

  4. 一个必用的javascript框架:underscore.js - wine的思考 - ITeye技术网站

    AngularJS+JqueryMobile+PhoneGap 打造APP « Dogeek AngularJS+JqueryMobile+PhoneGap 打造APP

  5. ubuntu无法解析主机错误与解决的方法

    今天在用命令行进行操作的时候,出现了无法解析主机的错误.google了一下,原来是hosts文件的问题.更改过来即可了 进入终端,输入 sudo gedit /etc/hosts.输入password ...

  6. iPhone5C三大看点:性能不输iPhone5 或售3399元

    乐杨俊编辑修改转载: iPhone 5C的发售时间或最早在9月18日,抢在中秋节前:最迟至国庆十一假期期间. [IT商业新闻网综合讯](记者 林涛)苹果2013年秋季发布会还有几个小时即将开幕,除了i ...

  7. [置顶] 浅谈Android的资源编译过程

    Android APK 一.APK的结构以及生成 APK是Android Package的缩写,即Android application package文件或Android安装包.每个要安装到Andr ...

  8. MFC 窗口重绘问题

    在客户区画直线等图形时, 发现当其最小化或者其他窗口遮挡时,出现窗口重绘,而将原来绘制的图形删除,上网上搜索知道,绘制图形的代码必须放置在Ondraw函数中,才能避免重绘时图形消失(因为一直在响应WM ...

  9. Codeforces Round #311 (Div. 2)

    我仅仅想说还好我没有放弃,还好我坚持下来了. 最终变成蓝名了,或许这对非常多人来说并不算什么.可是对于一个打了这么多场才好不easy加分的人来说,我真的有点激动. 心脏的难受或许有点是由于晚上做题时太 ...

  10. UVA 644 Immediate Decodability (字符处理)

    An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the ...