E. Mike and Foam(容斥原理)「建议收藏」

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


E. Mike and Foam(容斥原理)「建议收藏」

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 E. Mike and Foam(容斥原理)「建议收藏」 where E. Mike and Foam(容斥原理)「建议收藏」 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级别。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116365.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据结构与算法排序算法_数据结构快速排序图解

    数据结构与算法排序算法_数据结构快速排序图解什么是排序?排序是计算机内经常进行的一种操作,其目的是将一组“无序”的记录序列调整为“有序”的记录序列。1.排序的分类排序分为两类:内部排序:若整个排序过程不需要访问外存便能完成,则称此类排序

    2022年8月16日
    3
  • idea中edit configuration_pycharm和vim哪个好

    idea中edit configuration_pycharm和vim哪个好keyconflictswithIDEIDEmenu:file->settings->othersettings->VimEmulationconfigfile~/.ideavimrcusesystemclipboardasunnamedregister~/.ideavimrc:setclipboard^=unnamed,unnamedplus

    2022年10月1日
    0
  • python filelock 文件锁_详解进程文件锁FileLock

    python filelock 文件锁_详解进程文件锁FileLockimportjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.RandomAccessFile;importjava.nio.ByteBuffer;importjava.nio.channels.FileChannel;importjava.nio.channels.FileLock;import…

    2022年6月28日
    90
  • sql注入 报错注入_sql原理

    sql注入 报错注入_sql原理sql注入报错注入原理详解前言我相信很多小伙伴在玩sql注入报错注入时都会有一个疑问,为什么这么写就会报错?曾经我去查询的时候,也没有找到满意的答案,时隔几个月终于找到搞清楚原理,特此记录,也希望后来的小伙伴能够少走弯路0x01我们先来看一看现象,我这里有一个users表,里面有五条数据:然后用我们的报错语句查询一下:selectcount(*…

    2022年9月30日
    0
  • 调和数列举例_数列专项训练

    调和数列举例_数列专项训练算法训练调和数列问题时间限制:1.0s内存限制:512.0MB时间限制:1.0s内存限制:512.0MB问题描述输入一个实数x,求最小的n使得,1/2+1/3+1/4+…+1/(n+

    2022年8月5日
    3
  • 再见PowerDesigner,这款国人开源的数据库设计工具Chiner真香

    再见PowerDesigner,这款国人开源的数据库设计工具Chiner真香再见PowerDesigner,这款国人开源的数据库设计工具Chiner真香当我们在项目开发初期时,往往需要设计大量的表,此时使用数据库设计工具就会比较高效!今天给大家推荐一款国人开源的数据库设计工具chiner,界面漂亮,功能强大,希望对大家有所帮助!回顾PowerDesigner相信平时工作中,大家或多或少会使用PowerDesigner来设计数据库,感觉这款工具界面有点古老,界面看着就具年代感,有时候用起来也比较重,来看下之前使用它设计数据库的效果。最近体验了一把chiner,设计数据库

    2022年7月27日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号