题意:
解法:
考虑加入一个x,对答案的贡献,贡献=与x互质的数个数.
与x互质的数个数可以容斥来求:
设tot为数的总个数,那么与x互质的数个数num=tot-与x不互质的数个数.
计算出x的每种质因子pi,
因为x<=5e5,而2*3*5*7*9*11*13*17>5e5,因此质因子最多7种.
二进制枚举x的质因子组合进行容斥即可.
与x不互质的数个数
=与x有至少一个公共质因子的个数-与x有至少两个公共质因子的个数...
每次修改,二进制枚举计算贡献即可.
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=2e6+5;
vector<int>fac[maxm];
int mark[maxm];
int cnt[maxm];
int a[maxm];
int n,q;
int ans;
int tot;
void update(int x,int op){
int n=fac[x].size();
for(int i=0;i<(1<<n);i++){
int p=1;
for(int j=0;j<n;j++){
if(i>>j&1){
p*=fac[x][j];
}
}
cnt[p]+=op;
}
}
void ask(int x,int op){
int n=fac[x].size();
int temp=0;
for(int i=1;i<(1<<n);i++){
int p=1;
int f=0;
for(int j=0;j<n;j++){
if(i>>j&1){
p*=fac[x][j];
f^=1;
}
}
if(f){
temp+=cnt[p];
}else{
temp-=cnt[p];
}
}
ans+=(tot-temp)*op;
}
void solve(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
int x=a[i];
for(int j=2;j*j<=x;j++){
if(x%j==0){
while(x%j==0)x/=j;
fac[i].push_back(j);
}
}
if(x!=1)fac[i].push_back(x);
}
while(q--){
int x;cin>>x;
if(!mark[x]){
mark[x]=1;
ask(x,1);
update(x,1);
tot++;
}else{
mark[x]=0;
tot--;
update(x,-1);
ask(x,-1);
}
cout<<ans<<endl;
}
}
signed main(){
ios::sync_with_stdio(0);
solve();
return 0;
}