n个数 q个查询操作
每次操作对第x个数进行操作 如果序列里面没有这个数 就把这个数加进去 如果有就取出来
问在序列中有多少组数 满足互质的情况
先预处理出数据范围内所有数 所包含的约数
对于每次操作 同一个数 先是添加 然后删除。。。
用一个数组cnt来记录所有数是当前序列中几个数的公约数
然后通过枚举 容斥处理 得到答案
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 500010
#define MAXQ 200010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
vector<int> pri[MAXN];
int a[MAXQ];
int cnt[MAXN];
void init()
{
for(int i=2;i<MAXN;i++)
{
if(pri[i].empty())
{
for(int j=i;j<MAXN;j+=i)
pri[j].push_back(i);
}
}
}
int query(int x)
{
vector<int> vec=pri[x];
int sz=vec.size();
// cout<<"xxx "<<x<<" sz "<<sz<<endl;
int ans=0;
for(int i=0;i<(1<<sz);i++)//枚举 x的约数的情况 sz位 如果该位为1 表示这位上相应的约数存在
{
int y=1;
int f=1;
for(int k=0;k<sz;k++)
{
if((i>>k)&1)//i值的二进制表示里面有多少个1 奇数个f=-1
{
y*=vec[k];
f*=-1;
}
}
ans+=f*cnt[y];
}
vec.clear();
return ans;
}
void add(int x,int y)//y为1 表示把x添加到序列中 为-1表示从序列中删除
{
vector<int> vec=pri[x];
int sz=vec.size();
for(int i=0;i<(1<<sz);i++)
{
int z=1;
for(int k=0;k<sz;k++)
{
if((i>>k)&1)
z*=vec[k];
}
cnt[z]+=y;
}
vec.clear();
}
int flag[MAXQ];
int main()
{
// fread;
init();
int n,q;
while(scanf("%d%d",&n,&q)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
MEM(flag,0);
ll ans=0;
while(q--)
{
int x;
scanf("%d",&x);
x--;
if(!flag[x])
{
ans+=query(a[x]);
add(a[x],1);
flag[x]=1;
}
else
{
add(a[x],-1);
ans-=query(a[x]);
flag[x]=0;
}
cout<<ans<<endl;
}
}
return 0;
}