E. Mike and Foam
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

可以知道,一个500000范围内的数,不同的质因子的个数不会超过6个,因而可以考虑容斥原理解此题。

首先预处理出范围内的数所包含的不同质因子的个数及其中的质因子。设公约数包含某个质因子的对数为性质P,(P1,P2,P3,....),运用容斥原理则可解答此题了。其中,统计某个性质P的对数可以设一个数组预先把相应的对数的个数先存下来,这样可以简化计算。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std; const int N=500010; int fac[N][10],cf[N];
LL mul[N];
int arr[N],cnum[500],c;
bool vis[N];
LL tot=0,ans=0,curans; void initial(){
memset(cf,0,sizeof(cf));
for(int i=2;i<N;i++){
if(cf[i]) continue;
for(int j=i;j<N;j+=i){
fac[j][++cf[j]]=i;
}
}
} void dfs(int num,int pos,int cnt,int val){
if(pos>cf[num]){
cnum[c++]=val;
if(cnt&1) curans-=mul[val];
else curans+=mul[val];
return ;
}
dfs(num,pos+1,cnt,val);
dfs(num,pos+1,cnt+1,val*fac[num][pos]);
} void gao(int num){
c=0;
curans=0;
dfs(num,1,0,1);
} int main(){
initial();
int n,q,x;
while(scanf("%d%d",&n,&q)!=EOF){
for(int i=1;i<=n;i++){
scanf("%d",&arr[i]);
vis[i]=false;
}
memset(mul,0,sizeof(mul));
for(int i=1;i<=q;i++){
scanf("%d",&x);
if(arr[x]==1){
if(!vis[x]){
vis[x]=true;
ans+=tot;
tot++;
mul[1]++;
}
else {
vis[x]=false;
ans-=tot-1;
tot--;
mul[1]--;
}
cout<<ans<<endl;
continue;
}
// cout<<cf[arr[x]]<<endl;
if(!vis[x]){
vis[x]=true;
gao(arr[x]);
// cout<<curans<<endl;
ans+=curans;
tot++;
for(int i=0;i<c;i++)
mul[cnum[i]]++;
}
else{
vis[x]=false;
gao(arr[x]);
tot--;
ans-=curans;
for(int i=0;i<c;i++)
mul[cnum[i]]--;
}
cout<<ans<<endl;
}
}
return 0;
}

  

Codeforces Round #305 (Div. 2) E题(数论+容斥原理)的更多相关文章

  1. Codeforces Round #305 (Div. 2) C题 (数论)

    C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

  4. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  5. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  6. 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

  7. 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

    题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...

  8. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  9. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

随机推荐

  1. Java初级面试模拟1

    1.简单介绍一下你的项目,说一下项目有什么模块 2.说说常见的集合有哪些吧 答:Map接口和Collection接口是所有集合框架的父接口: Collection接口的子接口包括:Set接口和List ...

  2. HTML-ul分分钟理解

    在HTML中,列表有三种,如图分别是有序.无序和自定义列表.上面是我在网络上找到的一张图片很明了就看以看出来,今天要分享的就是其中的无序列表Ul(unordered list),给大家整理了一下我所知 ...

  3. Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树

    思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用  lazy=0 没被覆盖过 els ...

  4. System.Data.SqlClient.SqlException: 在向服务器发送请求时发生传输级错误。 (provider: TCP 提供程序, error: 0 - 远程主机强迫关闭了一个现有的连接。) .

    今天使用sql server 2008 R2管理器,进行SQL查询时,频率非常高的报错: System.Data.SqlClient.SqlException: 在向服务器发送请求时发生传输级错误. ...

  5. easyui form.rest和clear 重置表单和清除表单数据区别

    easyui中的一般我们在新增和编辑的时候 都是用一个form表单 那新增的时候 需要重置下表单内容,一般用 $('#EditForm').form('reset'); 大部分时候没问题,但是如果表单 ...

  6. hibernate.cfg.xml配置

    hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...

  7. ASP.net参数传递总结

    同一页面.aspx与.aspx.cs之间参数传递 1. .aspx.cs接收.aspx的参数:由于.aspx和.aspx.cs为继承关系,所以.aspx.cs可以直接对.aspx中的ID进行值提取,具 ...

  8. Apache服务器防范DoS

    Apache服务器对拒绝服务攻击的防范主要通过软件Apache DoS Evasive Maneuvers Module  来实现.它是一款mod_access的替代软件,可以对抗DoS攻击.该软件可 ...

  9. ProE常用曲线方程:Python Matplotlib 版本代码(玫瑰曲线)

    Pyplot教程:https://matplotlib.org/gallery/index.html#pyplots-examples 玫瑰曲线 文字描述 平面内,围绕某一中心点平均分布整数个正弦花瓣 ...

  10. CNN结构:色彩空间建模-色彩空间分析

    原文: 色彩空间基础 好一个NB的知乎专栏:色彩空间基础 第一章:色彩空间基础 关于色彩分析,引出了专门的数学基础.整个过程给出了完备的数学阐述,虽然没有试验数据,论述的相当精彩. 摘抄出一段:  上 ...