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 1 to n. i-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 a and b.
Mike is tired. So he asked you to help him in performing these requests.
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.
For each query, print the answer for that query in one line.
5 6 1 2 3 4 6 1 2 3 4 5 1
0 1 3 5 6 2
思路:对于每个数,统计与他不互质的有多少个,每个数分解质因子后,因为每个数的质因子最多有6个,所以状态压缩进行容斥统计,这个跟2014年鞍山站的Coprime很像
By gaolee, contest: Codeforces Round #305 (Div. 1), problem: (C) Mike and Foam, Accepted, #
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=500010;
int N,M;
int a[maxn];
int prime[maxn],vis[maxn];
int num,cnt[maxn];
bool is_in[maxn];
int sum;
void getprime()
{
num=0;
vis[1]=1;
for(int i=1;i<maxn;i++)
{
if(!vis[i])prime[num++]=i;
for(int j=0;j<num;j++)
{
if(i*prime[j]>=maxn)break;
vis[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
}
LL solve(int x)
{
vector<int> q;
for(int i=0;i<num&&prime[i]<=x;i++)
{
if(x%prime[i]==0)q.push_back(prime[i]);
while(x%prime[i]==0)x/=prime[i];
if(!vis[x]){q.push_back(x);break;}
}
int len=q.size();
LL ans=0;
for(int S=1;S<(1<<len);S++)
{
int p=0;
int u=1;
for(int i=0;i<len;i++)
if(S&(1<<i))p++,u*=q[i];
if(p&1)ans=ans+cnt[u];
else ans-=cnt[u];
}
if(ans)ans--;
return sum-1-ans;
}
void cal(int x,int val)
{
for(int j=1;j*j<=x;j++)
if(x%j==0)
{
cnt[j]+=val;
if(x/j!=j)cnt[x/j]+=val;
}
}
int main()
{
getprime();
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=N;i++)
scanf("%d",&a[i]);
memset(is_in,0,sizeof(is_in));
LL ans=0;
sum=0;
while(M--)
{
int x;
scanf("%d",&x);
if(is_in[x])
{
ans-=solve(a[x]);
cal(a[x],-1),is_in[x]=0;
sum--;
}
else
{
sum++;
cal(a[x],1),is_in[x]=1;
ans+=solve(a[x]);
}
cout<<ans<<endl;
}
}
return 0;
}