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)
上一篇 2022年1月22日 下午1:00
下一篇 2022年1月22日 下午1:00


相关推荐

  • VUE响应式原理-Dep类「建议收藏」

    VUE响应式原理-Dep类「建议收藏」classDep{constructor(){//存储订阅者this.subs=[]}//添加订阅者addSub(sub){if(sub&&sub.update){this.subs.push(sub)}}//通知订阅者notify(){//变量订阅者,执行更新this.subs.forEach(item=>item.update()).

    2022年5月26日
    35
  • SCSA—信息安全概述

    SCSA—信息安全概述数字化时代威胁升级:攻击频发、传统安全防护逐渐失效、安全风险能见度低、缺乏自动化防御手段一、信息安全概述:1)信息安全:防止任何对数据进行未授权访问的措施,或者防止造成信息有意无意泄漏、破坏、丢失等问题的发生,让数据处于远离危险、免于威胁的状态或特性2)网络安全:计算机网络环境下的信息安全二、信息安全的脆弱性及常见安全攻击1.网络的开放性:互联网的美妙之处在于你与每一个相连,它的可怕之处在于每一个人与你相连2.协议栈的脆弱性及常见攻击1)协议栈的自身脆弱性:缺乏数据源验证机制、缺乏机密性保障机

    2022年6月20日
    35
  • 如何让女朋友微笑—陪伴表白机器人

    如何让女朋友微笑—陪伴表白机器人

    2021年9月17日
    64
  • css透明度兼容性

    css透明度兼容性出校门快两年了 自己

    2026年3月19日
    3
  • 数据库锁表与解锁_数据库解锁

    数据库锁表与解锁_数据库解锁关键字:数据库锁表与解锁一、mysql锁定表:LOCKTABLEStbl_name{READ|WRITE},[tbl_name{READ|WRITE},…]解锁表:UNLOCKTABLES例子:LOCKTABLEStable1WRITE,table2READ…更多表枷锁;说明:1、READ锁代表其他用户只能读不能其他操作

    2022年8月23日
    15
  • Bizcharts 自定义tooltip 显示内容

    Bizcharts 自定义tooltip 显示内容需求 将英文换成汉字 Chartheight 300 width 800 scale scale data mydata onGetG2Insta chart chartIns chart gt Chartheight 300 width 800 scale scale data mydata

    2026年3月19日
    2

发表回复

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

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