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 1to ni-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 aand 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

题意:
输入n,m,然后输入n个数,之后是m次操作,每次操作输入一个下标i。下标i第一次出现,代表把数组第i个数放进架子中,第二次出现,代表取出来,
每次操作完之后。输出架子中的数字钟。有几个是互质的

思路:
先取出全部质因子,这样能够大大降低计算时间,枚举质因子的过程时间消耗差点儿能够忽略不计
然后使用质因子得到其它因子,而且记录个数来计算最后的值

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 500005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x) LL n,q;
LL ans;
bool vis[N];
LL a[N],s[N];
LL num[1000005],tot,cnt;
//num[i]记录包括因子i的数的个数 void _set(LL n)//求出质因子
{
LL i;
tot = 0;
for(i = 2; i<=n/i; i++)
{
if(n%i==0)
{
s[tot++] = i;
while(n%i==0)
n/=i;
}
}
if(n!=1)
s[tot++] = n;
} LL _add()
{
LL i,j,k;
LL ret = 0;
for(i = 1; i<(1<<tot); i++)//枚举状态
{
LL mul = 1,c = 0;
for(j = 0; j<tot; j++)
{
if((1<<j)&i)
mul*=s[j],c++;
}
if(c%2) ret+=num[mul];//由于偶数个质数相乘得出的数量能依据奇数得到,所以採用奇数添加,偶数减去的方法来使得总数不变
else ret-=num[mul];
num[mul]++;
}
ans += (cnt-ret);//总数减去不互质的对数
cnt++;
return ans;
} LL _sub()
{
LL i,j,k;
LL ret = 0;
for(i = 1; i<(1<<tot); i++)
{
LL mul = 1,c = 0;
for(j = 0; j<tot; j++)
{
if((1<<j)&i)
mul*=s[j],c++;
}
num[mul]--;
if(c%2) ret+=num[mul];
else ret-=num[mul];
}
cnt--;
ans -= (cnt-ret);
return ans;
} int main()
{
LL i,j,k,x;
cnt = ans = 0;
scanf("%I64d%I64d",&n,&q);
for(i = 1; i<=n; i++)
scanf("%I64d",&a[i]);
while(q--)
{
scanf("%I64d",&x);
_set(a[x]);
if(vis[x])
{
vis[x] = false;
printf("%I64d\n",_sub());
}
else
{
vis[x] = true;
printf("%I64d\n",_add());
}
} return 0;
}

Codeforces548E:Mike and Foam的更多相关文章

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

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

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

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

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

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

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

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

  6. Mike and Foam(位运算)

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

  7. codeforces #305 C Mike and Foam

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

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

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

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

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

随机推荐

  1. VIM使用技巧1

    .命令是vim中很重要的一个命令,用法如下: 加入有一个文件vimtest.txt,内容如下: 1 Line one  2 Line two                               ...

  2. Java解决跨域的方案

    在后台加上,在数据返回之前添加 response.setHeader("Access-Control-Allow-Origin","*"); 就可以了,前台不用 ...

  3. Dreamweaver安装须知

    1.断网安装,否则让你登录邮箱什么的,安装完成后退出,先不要运行: 2.将破解的文件直接复制到安装的文件的地方覆盖:(将32文件夹下的amtlib.dll复制到安装完毕的dw_cs6下,覆盖原来的am ...

  4. git的使用01

    直接下载安装git,这里就不演示了,如果安装成功,在桌面任意空白处单击鼠标右键,会多出两个选项 Git Gui Here和Git Bash Here,我们一般使用git bash here 右键之后点 ...

  5. JAVA线程池调优

        在JAVA中,线程可以使用定制的代码来管理,应用也可以利用线程池.在使用线程池时,有一个因素非常关键:调节线程池的大小对获得最好的性能至关重要.线程池的性能会随线程池大小这一基本选择而有所不同 ...

  6. 负载均衡技术之-lvs

    LVS简介 Internet的快速增长使多媒体网络服务器面对的访问数量快速增加,服务器需要具备提供大量并发访问服务的能力,因此对于大负载的服务器来讲, CPU.I/O处理能力很快会成为瓶颈.由于单台服 ...

  7. CentOS下yum安装

    centos最小化安装不会装yum,以下是安装方法:(所有操作均在ROOT用户下,系统版本是centos7) 一.删除原有YUM # rpm -aq|grep yum|xargs rpm -e --n ...

  8. Git 使用指南(cmd + gui)

    git 使用简易指南http://www.bootcss.com/p/git-guide/ Git版本控制使用方法入门教程http://www.uml.org.cn/pzgl/201204285.as ...

  9. 随手看的一本书《java微服务》,测试成功了其中的第一个样例

    静态语言,JAVA应该多了解,结合微服务,DOCKER,再搞搞SPRING CLOUD,就能跟上时代了. 对了,链一个买书的地址: https://item.jd.com/12089180.html ...

  10. CF978E Bus Video System【数学/前缀和/思维】

    [链接]: CF [分析]: 设上车前人数 x ,中途最大人数为 x+max ,最小人数为 x+min (max≥0,min≤0) 可得不等式组 x+max≤w, x+min≥0 整数解个数为 max ...