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. cat /proc/maps 进程内存映射【转】

    转自:http://blog.csdn.net/fisher_jiang/article/details/5063852 proc/<PID>/maps查看进程的虚拟地址空间是如何使用的. ...

  2. Linux安转jdk

    1. 创建目录 > mkdir  /opt/java > cd /opt/java 2. 下载jdk压缩包到上述目录 jdk-8u162-linux-x64.tar.gz 3. 解压缩.建 ...

  3. centos编译安装vim并支持lua

    系统环境:centos6.5 x86 (basic server) 1.安装编译环境. # yum groupinstall "Development Tools" 2.安装vim ...

  4. python--math

    >>> import math >>> >>> # ceil,取大于等于x的最小的整数值 >>> math.ceil(4) 4 ...

  5. mysql 事务、游标

    mysql 事务 在MySQL中只有使用了Innodb数据库引擎的数据库或表才支持事务,所以很多情况下我们都使用innodb引擎. 事务处理可以用来维护数据库的完整性,保证成批的SQL语句要么全部执行 ...

  6. Netty源码学习(五)ChannelInitializer

    0. ChannelInitializer简介 直接用ChannelInitializer的注释吧:A special ChannelInboundHandler which offers an ea ...

  7. UpdateLayeredWindow后,使用Gdi DrawText文字透明的解决办法

    来源:http://stackoverflow.com/questions/5309914/updatelayeredwindow-and-drawtext 要点就是在先在memDc DrawText ...

  8. UAF学习之Adobe reader CVE-2013-3346

    学习了UAF,分析了几个漏洞,同时,也熟悉了windbg的用法,收获挺大. 基本的UAF分析流程如下: i:找有漏洞的函数 ii:找到被释放对象的类型,以及被释放对象在内存中的位置 iii:理解对象的 ...

  9. [HDU5528]Count a * b

    题目大意: 定义函数$f(m)=\displaystyle\sum_{a=0}^{m-1}\sum_{b=0}^{m-1}[m\nmid ab]$,$g(n)=\displaystyle\sum_{m ...

  10. sqlsever 和oracle的参数

    StringBuilder strSql = new StringBuilder(); strSql.Append("insert into YXZY_TSDQWH("); str ...