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. ubuntu14.04LTS openssh-server 手动安装配置步骤

    先用能上网的机器下载:zlib-1.2.5.tar.bz2.openssh-5.6p1.tar.gz.openssl-0.9.8o.tar.tar,接下来,准备安装. 步骤如下: 1.首先解压安装zl ...

  2. Centos 6.3防火墙端口放行

    vi /etc/sysconfig/iptables #防火墙增加 -A INPUT -p tcp -m state --state NEW -m tcp --dport -j ACCEPT serv ...

  3. Spring整合Disruptor3

    一.什么是Disruptor 从功能上来看,Disruptor 是实现了“队列”的功能,而且是一个有界队列.那么它的应用场景自然就是“生产者-消费者”模型的应用场合了. 可以拿 JDK 的 Block ...

  4. 设计模式-python实现

    设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情 ...

  5. hdu 5109(构造数+对取模的理解程度)

    Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. ReportView控件的使用

    ReportView控件的使用Posted on 2012-01-06 17:02 随遇 阅读(3006) 评论(0)  编辑 收藏 最近使用了ReportView控件绑定数据,总结下: 1.在设计器 ...

  7. POJ 2337 Catenyms (欧拉图)

    本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...

  8. 探究堆喷射(heap spray)

    博客园的自动保存系统真心不咋地,写的差不多的文章蓝屏之后就没有了,醉了! 浏览器是互联网世界最主要的软件之一,从IE6到IE11安全攻防在不断升级,防御措施的实施促使堆喷射技巧不断变化.写这篇博文想好 ...

  9. JAVA 父类与子类初始化顺序问题

    main方法-->子类对象的初始化语句(new className()语句)--->子类构造[因为继承的缘故,它先不会执行]--->父类构造[这一步先不会执行]--->父类静态 ...

  10. 【BZOJ4458】GTY的OJ

    题面 Description 身为IOI金牌的gtyzs有自己的一个OJ,名曰GOJ.GOJ上的题目可谓是高质量而又经典,他在他的OJ里面定义了一个树形的分类目录,且两个相同级别的目录是不会重叠的.比 ...