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. ubuntu下安装 gSOAP 用于C/C++开发web service服务端与客户端

    昨天在ubuntu下进行安装gSOAP,费了很多时间,没成功,今天又来找了大量教程资料,终于一次成功,这里写下自己的安装步骤和方法,供大家参考. 首先下载gsoap,我下载的是gsoap-2.8.1. ...

  2. Selenium2+python自动化9-CSS定位语法【转载】

    前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...

  3. ASP.NET增加微信公众号功能

    最近微信的公众号使用的挺多的.我们也弄了个,现在有个需求就是在用户点击菜单的时候,获取用户的OPENID,然后作为后续使用. 首先记录下步骤, 安装必要的dll引用 install-package s ...

  4. 先刷一波简单的web前端面试题

    1简述一下src与href的区别href 是指向网络资源所在位置,建立和当前元素(锚点)或当前文档(链接)之间的链接,用于超链接.src是指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置 ...

  5. 解决win10下微信开发者工具点击错位问题

    在系统设置->显示->更改文本.应用等项目的大小选项中将百分比改为100%即可.

  6. 牛客小白月赛3 B 躲藏【动态规划/字符串出现cwbc子序列多少次】

    链接:https://www.nowcoder.com/acm/contest/87/B来源:牛客网 XHRlyb和她的小伙伴Cwbc在玩捉迷藏游戏 Cwbc藏在多个不区分大小写的字符串中. 好奇的X ...

  7. 中矿新生赛 H 璐神看岛屿【BFS/DFS求联通块/连通块区域在边界则此连通块无效】

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 璐神现在有张n*m大小的地图,地图上标明了陆地(用 ...

  8. poj2728(最小比率生成树)

    poj2728 题意 给出 n 个点的坐标和它的高度,求一颗生成树使得树上所连边的两点高度差之和除以距离之和最小. 分析 01分数规划+最小生成树. 对于所有的边,在求最小生成树过程中有选或不选的问题 ...

  9. Moving Average from Data Stream -- LeetCode

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  10. centos忘记密码,重新设置密码的方法

    (1)重新启动Centos,在启动过程中,长按“ESC”键,进入GNU GRUB界面. (2)选择要进入的系统,按“E”键(在启动之前编辑命令). (3)选择第二项操作系统的内核“kernel”,按& ...