#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int N =500000+10;
vector<int> fac[N];
int vis[N],cnt[N],v[N];
int n,q,a[N];
ll ans;
int x;
void dfs(int u,int mul,int tp,int bo)
{
if(u==fac[x].size())
{
if(bo) cnt[mul]--;
ans+=tp*cnt[mul];//当前mul的倍数有多少个
if(!bo) cnt[mul]++;
return ;
}
dfs(u + 1, mul * 1, tp, bo);
dfs(u + 1, mul * fac[x][u], -tp, bo);
}
int main()
{
//分解质因子
for(int i=2;i<N;i++)
{
if(v[i]) continue;
for(int j=i;j<N;j+=i)
{
v[j]=1;
fac[j].push_back(i);
}
}
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
while(q--)
{
scanf("%d",&x);
if(vis[x])
{
vis[x]=0;
x=a[x];
dfs(0,1,-1,1);
}
else
{
vis[x] = 1;
x = a[x];
dfs(0, 1, 1, 0);
}
printf("%I64d\n", ans);
}
}
#include<stdio.h>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int N =500000+10;
vector<int> fac[N];
int vis[N],cnt[N],v[N];
int n,q,a[N];
ll ans;
int x;
void dfs(int u,int mul,int tp,int bo)
{
if(u==fac[x].size())
{
if(bo) cnt[mul]--;
ans+=tp*cnt[mul];//当前mul的倍数有多少个
if(!bo) cnt[mul]++;
return ;
}
dfs(u + 1, mul * 1, tp, bo);
dfs(u + 1, mul * fac[x][u], -tp, bo);
}
int main()
{
//分解质因子
for(int i=2;i<N;i++)
{
if(v[i]) continue;
for(int j=i;j<N;j+=i)
{
v[j]=1;
fac[j].push_back(i);
}
}
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
while(q--)
{
scanf("%d",&x);
if(vis[x])
{
vis[x]=0;
x=a[x];
dfs(0,1,-1,1);
}
else
{
vis[x] = 1;
x = a[x];
dfs(0, 1, 1, 0);
}
printf("%I64d\n", ans);
}
}