E. Mike and Foam(容斥原理)

E. Mike and Foam

Mike is a bartender at Rico’s bar. At Rico’s, they put beer glasses in a special shelf. There aren kinds of beer at Rico’s numbered from 1 to n. i-th kind of beer hasai 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 numberx. 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 thati<j and where is the greatest common divisor of numbersa 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 andq (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 integerx (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 61 2 3 4 6123451

Output

013562

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

题解:题目可以转化为,在一个动态的集合中,没加入一个数,或取出一个数后,剩下的数中互质的有多少对,注意是无序的。 因为在题目的数据范围内质因子的数量很少,复杂度基本上可以忽略不计,所以我们可以对每一个新加进去的数,或者取出来的数进行质因子分解。然后通过容斥原理,先算出集合中有多少个与其不互质,这个很容易算出来吧! (如果想不出来可以看下我的代码,就是通过一个数组来计数,挺简单)。知道了不互质的,自然就知道互质了辣! 然后就答案减去或者加上互质的数就可以了。 时间复杂度估计10^7级别。

,快乐不是因为拥有的多而是计较的少

E. Mike and Foam(容斥原理)

相关文章:

  • 【算法】直接插入排序C语言实现
  • 嵌入式 FAAC1.28 在海思HI3518C/HI3518A平台linux中的编译优化
  • Android 动画animation 深入分析
  • Mybatis极其(最)简(好)单(用)的一个分页插件
  • 你感兴趣的文章:

    标签云:

    亚洲高清电影在线, 免费高清电影, 八戒影院夜间, 八戒电影最新大片, 出轨在线电影, 午夜电影院, 在线影院a1166, 在线电影院, 在线观看美剧下载, 日本爱情电影, 日韩高清电影在线, 电影天堂网, 直播盒子app, 聚合直播, 高清美剧, 高清美剧在线观看 EhViewer-E站, E站, E站绿色版, qqmulu.com, qq目录网, qq网站目录,