#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
typedef unsigned long long ull;
typedef short int int16;
typedef long double ldouble;

typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

typedef pair < int , int > ipair;
typedef pair < LL , LL > lpair;
typedef pair < int , int > pii;
typedef pair < LL , LL > pll;
typedef pair<double, double> pdd;

typedef map < int,int > imap;
typedef map < LL , LL > lmap;
typedef map < char , int > cimap;
typedef map < string , int > simap;

typedef vector < int > ivector;
typedef vector < LL > lvector;
typedef vector < string > svector;

typedef set < int > iset;
typedef set < LL > lset;
typedef set < char > cset;
typedef set < string > sset;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

const int size = 1e5 + 5;
const double eps = 0.0001;
const ldouble PI = 3.1415926535897932384626433832795;
const LL  MOD = 1000000007;
const LL INF = 1LL << 62;
const LL MAX5 = 100001;
const LL MAX6 = 1000001;
const LL MAX17 = 99999999999999999;
const double DMAX = 2e18+5;
const int P = 239017;

#define MEMS(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(min(a,b),min(c,d))
#define max4(a,b,c,d)   max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define max6(a,b,c,d,e,f) max(max3(a,b,c),max3(d,e,f))
#define min6(a,b,c,d,e,f) min(min3(a,b,c),min3(d,e,f))
#define all(x) (x).begin(), (x).end()
#define rall(c) (c).rbegin(), (c).rend()
#define rep(i,a,n) for (long long i=a;i<n;++i)
#define per(i,n,a) for (long long i=n-1;i>=a;--i)
#define read(a,n) for(LL i=0;i<n;++i)cin>>a[i];
#define print(a,n) for(int i=0;i<n;++i)cout<<a[i]<<" ";
#define vout(); for(int i=0;i<res.size();++i)cout<<res[i]<<" ";
#define reunique(v) v.resize(unique(v.begin(), v.end()) - v.begin())
#define res(); cout<<res;
#define ones(a) __builtin_popcount(a);
#define onesll(a) __builtin_popcountll(a);
#define log2(val) logb(val)
void solution();
void include_file();

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//srand(time(0));
//include_file();
//#define mishatron

#ifdef mishatron
	clock_t z = clock();
#endif
    solution();
#ifdef mishatron
    printf("\n*** EXECUTION TIME: %.3lf***\n", (double)(clock() - z) / CLOCKS_PER_SEC);
#endif

return 0;
}
inline void include_file(){
	ios_base::sync_with_stdio(true);
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
}
inline bool isPrime(LL n) {
    for (int i = 2; i * i <= n; ++i)if (n % i == 0)return false;
    return true;
}
inline string ekv(string a) {
    if (a.size() & 1)return a;
    string x = ekv(a.substr(0, a.size() / 2));
    string y = ekv(a.substr(a.size() / 2));
    return min(x + y, y + x);
}
inline LL cubic_root(LL x) {
    LL l = 0, r = MAX6;
    while (l != r) {
        LL m = (l + r + 1) / 2;
        if (m * m * m > x)
            r = m - 1;
        else
            l = m;
    }
    return l;
}
inline float FastInvSqrt(float x) {//   1/sqrt(X)
  float xhalf = 0.5f * x;
  int i = *(int*)&x;  // i?aanoaaei aeou float a aeaa oaeiai ?enea
  i = 0x5f3759df - (i >> 1);  // eaeiai ?a?oa caanu i?ienoiaeo ?
  x = *(float*)&i;
  x = x*(1.5f-(xhalf*x*x));
  return x;
}
inline LL gcd(LL a,LL b)
{
	while(b ^= a ^= b ^= a = a % b);
	return a;
}
inline LL lcm(LL a, LL b)
{
  return a/gcd(a, b)*b;
}
unsigned long long NCR(unsigned long long n, unsigned long long r)
{
    if (r == 0) return 1;
    if (r > n>>1) return NCR(n, n-r); // save some computation
    unsigned long long res = 1;
    for(long long k = 1; k <= r; ++k)
    {
        res *= (unsigned long long)(n-k+1);
        res /= k;
    }
    return res;
}
//2^61-1
constexpr uint64_t mod = (1ull<<61) - 1;
inline uint64_t modmul(uint64_t a, uint64_t b){
	uint64_t l1 = (uint32_t)a, h1 = a>>32, l2 = (uint32_t)b, h2 = b>>32;
	uint64_t l = l1*l2, m = l1*h2 + l2*h1, h = h1*h2;
	uint64_t ret = (l&mod) + (l>>61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
	ret = (ret & mod) + (ret>>61);
	ret = (ret & mod) + (ret>>61);
	return ret-1;
}
void yes()
{
	cout<<"YES";
}
void no()
{
	cout<<"NO";
}

void yes(bool res, bool small=false)
{
	if(!small)
	{
		if(res)
		cout<<"YES";
		else cout<<"NO";
	}
	else
	{
		if(res)
		cout<<"Yes";
		else cout<<"No";
	}

}
void dabl(double x)
{
	printf("%.10lf",x);
}
namespace bits
{
	template<typename X> inline X MAX(const X& a, const X& b)
{ return b & ((a-b) >> 31) | a & (~(a-b) >> 31);}
template<typename X> inline X MIN(const X& a, const X& b)
{ return a & ((a-b) >> 31) | b & (~(a-b) >> 31);}

//i?iaa?ea ia noaiaiu 2
inline bool check2(const LL n)
{
	return 	n > 0 && (n & (n - 1));
}
//inoaoie io aaeaiey 2^n ia m
inline LL ostatok2(const LL n, const LL m)
{
	return m & (n-1);
}
template<typename X>
inline void SWAP(X &a, X &b)
{
	a^=b;
	b^=a;
	a^=b;
}

inline size_t count_1_in_LL(unsigned long long n)
{
    std::size_t i(0);
    for (;n;++i) n&=n-1;
    return i;
}
}
// deg[] = {1, P, P^2, P^3, ...}
// h[] = {0, s[0], s[0]*P + s[1], s[0]*P^2 + s[1]*P + s[2], ...}
/*unsigned long long h[n + 1], deg[n + 1];
h[0] = 0, deg[0] = 1;
for (int i = 0; i < n; i++) {
  h[i + 1] = h[i] * P + s[i];
  deg[i + 1] = deg[i] * P;
}
auto get_hash = [&]( int l, int r ) { // [l..r]
  return h[r + 1] - h[l] * deg[r - l + 1];
};*/
void debug() {cout<<endl;}
template <typename H, typename... T> void debug(H p, T... t) {
   std::cout << p<<" ";
   debug(t...);
}
vector <vector<int> > g;
///////////////////SOLUTION

const int N = 1e5 + 10, LOG = 60;
inline void solution()
{
	int t; cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		vector<ll> a(n);
		for (auto& i : a) {
			cin >> i;
		}
		vector<ll> cnt(LOG, 0);
		for (auto& v : a) {
			for (int b = 0; b < LOG and v; ++b) {
				cnt[b] += v % k;
				v /= k;
			}
		}
		if (*max_element(cnt.begin(), cnt.end()) < 2) {
			cout << "YES\n";
		} else {
			cout << "NO\n";
		}
	}
}
///////////////////SOLUTION


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(100,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0ll)
    {
       long long dig=tmp%k;
       if((dig!=0ll)&&(dig!=1ll))
         return -1;
       tmp=tmp/k;
    }
     tmp=n;
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1ll)
ans[place]=1;
}
else
{
if(dig==1ll)
return -1;

}

place++;
    tmp=tmp/k;

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;
       break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int i=0;i<100;i++)
ans[i]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(48,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0)
    {
       long long dig=tmp%k;
       if((dig!=0)&&(dig!=1))
         return -1;
       tmp=tmp/k;
    }
     tmp=n;
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1)
ans[place]=1;
}
else
{
if(dig==1)
return -1;

}

place++;
    tmp=tmp/k;

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int k=convertTokandones(curr,k);
       if(k==-1)
       {
        cout<<"NO\n";flag=0;
break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int i=0;i<48;i++)
ans[i]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(48,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0)
    {
       long long dig=tmp%k;
       if((dig!=0)&&(dig!=1))
         return -1;
       tmp=tmp/k;
    }
     tmp=n;
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1)
ans[place]=1;
}
else
{
if(dig==1)
return -1;

}

place++;
    tmp=tmp/k;

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;
break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int i=0;i<48;i++)
ans[i]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(100,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0ll)
    {
       long long dig=tmp%k;
       if((dig!=0ll)&&(dig!=1ll))
         return -1;
       tmp=tmp/k;
    }
     tmp=n;
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1ll)
ans[place]=1;
}
else
{
if(dig==1ll)
return -1;

}

place++;
    tmp=tmp/k;

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;
       break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int j=0;j<100;j++)
ans[j]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(100,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
     if(k!=0)
     {
    while(tmp!=0ll)
    {
       long long dig=tmp%k;
       if((dig!=0ll)&&(dig!=1ll))
         return -1;
       tmp=tmp/k;
    }
     }
     tmp=n;
     if(k!=0)
     {
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1ll)
ans[place]=1;
}
else
{
if(dig==1ll)
return -1;

}

place++;
    tmp=tmp/k;

}
     }
     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;
       break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int j=0;j<100;j++)
ans[j]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(100,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0)
    {
       long long dig=tmp%k;
       if((dig!=0)&&(dig!=1))
         return -1;
       tmp=tmp/k;
    }
     tmp=n;
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1)
ans[place]=1;
}
else
{
if(dig==1)
return -1;

}

place++;
    tmp=tmp/k;

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;
break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int i=0;i<100;i++)
ans[i]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(48,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0)
    {
       long long dig=tmp%k;
       if((dig!=0)&&(dig!=1))
         return -1;

    }
     tmp=n;
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1)
ans[place]=1;
}
else
{
if(dig==1)
return -1;

}

place++;

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int k=convertTokandones(curr,k);
       if(k==-1)
       {
        cout<<"NO\n";flag=0;
break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int i=0;i<48;i++)
ans[i]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(100,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0ll)
    {
       long long dig=tmp%k;
       if((dig!=0ll)&&(dig!=1ll))
         return -1;
       tmp=tmp/k;
    }
     tmp=n;
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1ll)
ans[place]=1;
}
else
{
if(dig==1ll)
return -1;

}

place++;
    tmp=tmp/k;

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
      if(flag!=0)
      {
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;

       }
      }

}
if(flag==1)
cout<<"YES\n";
for(int j=0;j<100;j++)
ans[j]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(100,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0;
    while(tmp!=0ll)
    {
        if(k!=0)
        {
       long long dig=tmp%k;
       if((dig!=0ll)&&(dig!=1ll))
         return -1;
       tmp=tmp/k;
        }
    }
     tmp=n;
while(tmp!=0)
{
    if(k!=0)
    {
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1ll)
ans[place]=1;
}
else
{
if(dig==1ll)
return -1;

}

place++;
    tmp=tmp/k;
    }

}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;
       break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int j=0;j<100;j++)
ans[j]=0;
}

}


#include<bits/stdc++.h>
using namespace std;

vector<int> ans(100,0);
int convertTokandones(long long n,long long k)
{

     long long tmp=n;

     int place=0
     if(k!=0)
     {
    while(tmp!=0ll)
    {
       long long dig=tmp%k;
       if((dig!=0ll)&&(dig!=1ll))
         return -1;
       tmp=tmp/k;
    }
     }
     tmp=n;
     if(k!=0)
     {
while(tmp!=0)
{
long long dig=tmp%k;

if(ans[place]==0)
{
if(dig==1ll)
ans[place]=1;
}
else
{
if(dig==1ll)
return -1;

}

place++;
    tmp=tmp/k;

}
}

     return 1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;long long k;
cin>>n;
cin>>k;

int flag=1;
for(int i=0;i<n;i++)
{
      long long curr;
      cin>>curr;
       int num=convertTokandones(curr,k);
       if(num==-1)
       {
        cout<<"NO\n";flag=0;
       break;
       }

}
if(flag==1)
cout<<"YES\n";
for(int j=0;j<100;j++)
ans[j]=0;
}

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count=0;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            count=0;
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[0]++;
            else if(a[i]%k==0)
            {
                while(a[i]>0)
                {
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    mp[count]++;
                    if(mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            else
                {
                    a[i]--;
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    if(a[i]==1)
                    {
                        mp[0]++;
                    mp[count]++;
                    }
                    else
                    {chh=0;
                    break;}
                }
            if(mp[0]>1||chh==0)
            {
                chh=0;
                break;
            }
        }
        if(chh==1)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[1]++;
            else
            {
                while(a[i]>0)
                {
                    //count=0;
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    if(++mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            if(mp[1]>1||chh=0)
            {
                chh=0;
                break;
            }
        }
        if(chh)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[1]++;
            else
            {
                while(a[i]>0)
                {
                    count=0;
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    if(++mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            if(mp[1]>1)
            {
                chh=0;break;
            }
        }
        if(chh)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[1]++;
            else
            {
                while(a[i]>0)
                {
                    count=0;
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    if(++mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
        }
        if(chh)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count=0;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            count=0;
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[0]++;
            else
            {
                while(a[i]>0)
                {
                    if(a[i]%k!=0)
                    {
                        a[i]--;
                        mp[0]++;
                    }
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    mp[count]++;
                    if(mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            if(mp[0]>1||chh==0)
            {
                chh=0;
                break;
            }
        }
        if(chh==1)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count=0;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            count=0;
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[0]++;
            else if(a[i]%k==0)
            {
                while(a[i]>0)
                {
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    mp[count]++;
                    if(mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            else
                {
                    chh=0;
                    break;
                }
            if(mp[0]>1||chh==0)
            {
                chh=0;
                break;
            }
        }
        if(chh==1)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count=0;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[0]++;
            else
            {
                if(a[i]%k!=0)
                {
                    chh=0;
                    break;
                }
                while(a[i]>0)
                {
                    //count=0;
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    if(++mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            if(mp[0]>1||chh==0)
            {
                chh=0;
                break;
            }
        }
        if(chh)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count=0;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[0]++;
            else
            {
                while(a[i]>0)
                {
                    //count=0;
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    if(++mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            if(mp[0]>1||chh==0)
            {
                chh=0;
                break;
            }
        }
        if(chh)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0); cin.exceptions(cin.failbit);
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k,i,chh=1,ch,count=0;
        cin>>n>>k;
        ll a[n];
        unordered_map<ll,ll> mp;
        rep(i,0,n)
        cin>>a[i];
        rep(i,0,n)
        {
            if(a[i]==0)
            continue;
            else if(a[i]==1)
            mp[1]++;
            else
            {
                while(a[i]>0)
                {
                    //count=0;
                    while(a[i]%k==0)
                    {
                        count++;
                        a[i]=a[i]/k;
                    }
                    if(++mp[count]>1)
                    {chh=0;
                    break;}
                    a[i]--;
                    if(a[i]%k!=0)
                    {
                        chh=0;
                    break;}
                }
            }
            if(mp[1]>1||chh==0)
            {
                chh=0;
                break;
            }
        }
        if(chh)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long

ll cnt[105];

void convert(int n,int k) {
	int idx = 0;
	while(n > 0) {
		cnt[idx] += (n%k);
		idx++;
		n /= k;
	}
}

void solve() {
	memset(cnt,0,sizeof(cnt));
	int n,k; cin >> n >> k;
	ll x;
	for(int i = 0; i < n; i++) {
		cin >> x;
		convert(x,k);
	}
	for(int i = 0; i < 100; i++) {
		if(cnt[i] > 1) {
			cout << "NO\n";
			return;
		}
	}
	cout << "YES\n";
}

int main() {
	#ifdef LOCAL
	freopen("inp","r",stdin);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while(t--) solve();
}


#include <bits/stdc++.h>
using namespace std;

#define vi vector<ll>
#define ll long long

bool F;

vi convert(int n,int k) {
	vi res;
	while(n > 0) {
		int rem = n%k;
		if(rem > 1) {
			F = true;
			return vi{};
		}
		res.push_back(rem);
		n /= k;
	}
	return res;
}

void solve() {
	vi cnt(104,0);
	ll n,k; cin >> n >> k;
	vi a(n);
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for(int i = 0; i < n; i++) {
		vi c = convert(a[i],k);
		if(F) {
			cout << "NO\n";
			F= false;
			return;
		}
		for(int j = 0; j < c.size(); j++) {
			cnt[j] += c[j];
		}
	}
	if(*max_element(cnt.begin(),cnt.end()) > 1) cout << "NO\n";
	else cout << "YES\n";
}

int main() {
	#ifdef LOCAL
	freopen("inp","r",stdin);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while(t--) solve();
}


/****GL HF******/
/*Author :WID  */
/*Created Time :2015/5/28 21:15:09*/
/*File Name :e.cpp*/
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <vector>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
#define o(a) cout << #a << " = " << a << endl
#define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl
using namespace std;
typedef long long ll;
const int MAXN = 1e5;
int prime[5 * MAXN + 100];
int p[5*MAXN + 100][100];
int vis[5 * MAXN + 100];
int vis_times[5 * MAXN + 100];
int val[MAXN * 5 + 100];

void start(){
    for(int i = 2;i <= 5*MAXN;i++){
        if(!prime[i]) prime[++prime[0]] = i;
        for(int j = 1;j <= prime[0] && prime[j] * i <= 5 * MAXN;j++){
            prime[prime[j] * i] = 1;
            if(i % prime[j] == 0) break;
        }
    }
    for(int i = 1;i <= prime[0];i++){
        int k = prime[i];
        for(int j = k;j <= 5*MAXN;j+=k)
            p[j][++p[j][0]] = k;
    }
}

int solve(int n){
    int len = p[n][0],ret = 0;
    for(int i = 1;i <= (1 << len) - 1;i++){
        int tmp = 1;
        int cnt = 0;
        for(int j = 0;j <= len;j++)
            if(i & (1 << j)){
                tmp *= p[n][j+1];
                cnt++;
            }
        if(cnt & 1) ret += vis_times[tmp];
        else ret -= vis_times[tmp];
    }
    if(ret) ret--;
    return ret;
}

void cnt(int n,int val){
    for(int i = 1;i * i <= n;i++)
        if(n % i == 0){
            vis_times[i] += val;
            if(n / i != i) vis_times[n / i] += val;
        }
}

int main()
{
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    start();
    int n,m;
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
        scanf("%d",val+i);
    int ret = 0,ans = 0;
    for(int i = 1;i <= m;i++){
        int tmp;
        scanf("%d",&tmp);
        if(vis[tmp]){
            ans -= ret - 1 - solve(val[tmp]);
            vis[tmp] = 0;
            cnt(val[tmp],-1);
            ret--;
        }
        else{
            vis[tmp] = 1;
            cnt(val[tmp],1);
            ret++;
            ans += ret - 1 - solve(val[tmp]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 500005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

LL n,q;
LL ans;
bool vis[N];
LL a[N],s[N];
LL num[1000005],tot,cnt;
//num[i]��¼������i����ĸ���

void _set(LL n)//���������
{
    LL i;
    tot = 0;
    for(i = 2; i<=n/i; i++)
    {
        if(n%i==0)
        {
            s[tot++] = i;
            while(n%i==0)
                n/=i;
        }
    }
    if(n!=1)
        s[tot++] = n;
}

LL _add()
{
    LL i,j,k;
    LL ret = 0;
    for(i = 1; i<(1<<tot); i++)//ö��״̬
    {
        LL mul = 1,c = 0;
        for(j = 0; j<tot; j++)
        {
            if((1<<j)&i)
                mul*=s[j],c++;
        }
        if(c%2) ret+=num[mul];//��Ϊż���������˵ó��������ܸ������õ������Բ����������ӣ�ż���ȥ�ķ�����ʹ�������
        else ret-=num[mul];
        num[mul]++;
    }
    ans += (cnt-ret);//�����ȥ�����ʵĶ���
    cnt++;
    return ans;
}

LL _sub()
{
    LL i,j,k;
    LL ret = 0;
    for(i = 1; i<(1<<tot); i++)
    {
        LL mul = 1,c = 0;
        for(j = 0; j<tot; j++)
        {
            if((1<<j)&i)
                mul*=s[j],c++;
        }
        num[mul]--;
        if(c%2) ret+=num[mul];
        else ret-=num[mul];
    }
    cnt--;
    ans -= (cnt-ret);
    return ans;
}

int main()
{
    LL i,j,k,x;
    cnt = ans = 0;
    scanf("%I64d%I64d",&n,&q);
    for(i = 1; i<=n; i++)
        scanf("%I64d",&a[i]);
    while(q--)
    {
        scanf("%I64d",&x);
        _set(a[x]);
        if(vis[x])
        {
            vis[x] = false;
            printf("%I64d\n",_sub());
        }
        else
        {
            vis[x] = true;
            printf("%I64d\n",_add());
        }
    }

    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>

#define pb push_back

using namespace std;

const int N = 500000;

int mu[N + 5], prime[N], cnt[N + 5];
int v[N + 5];

int n, tot, id = 0;
int b[N + 5];
int a[300005], stack[300005];
bool vis[300005];

vector<int > e[300005];
int now, top = 0;

void get_prime()
{
    mu[1] = 1;
    v[1] = 1;
    for (int i = 2; i <= N; i ++)
    {
        if (!v[i]) v[i] = 1, prime[++ tot] = i, mu[i] = -1;
        for (int j = 1; j <= tot && prime[j] <= (int )(N / i); j ++)
        {
            v[prime[j] * i] = i;
            if (i % prime[j] == 0) { mu[i * prime[j]] = 0; break; }
            mu[i * prime[j]] = - mu[i];
        }
    }
}

void dfs(int x, int sum)
{
    if (x == top + 1)
    {
        if (b[sum] != id) e[now].pb(sum), b[sum] = id;
    }
    else
    {
        dfs(x + 1, sum * stack[x]);
        dfs(x + 1, sum);
    }
}

int main( )
{
    int q, x;
    scanf("%d %d", &n, &q);
    get_prime();
    for (int i = 1; i <= n; i ++)
    {
        scanf("%d", &a[i]);
        int tmp = a[i];
        top = 0;
        ++ id;
        while (tmp != 1)
        {
            stack[++ top] = tmp / v[tmp];
            tmp = v[tmp];
        }
        now = i;
        dfs(1, 1);
    }
    long long ans = 0;
    for (int i = 1; i <= q; i ++)
    {
        scanf("%d", &x);
        if (!vis[x])
        {
            vis[x] = true;
            for (int j = 0; j < (int )(e[x].size()); j ++)
            {
                int to = e[x][j];
                ans += mu[to] * cnt[to];
                ++ cnt[to];
            }
        }
        else
        {
            vis[x] = false;
            for (int j = 0; j < (int )(e[x].size()); j ++)
            {
                int to = e[x][j];
                -- cnt[to];
                ans -= mu[to] * cnt[to];
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


//ios::sync_with_stdio(false);
using namespace std;
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#define sqr(x) (x)*(x)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define ll long long
long long t,a[100005],n,m,i,j,x,s;
set<long long> b;
map<int,int> c;
vector<long long> d;
void fj(long long x)
{
	d.clear();
	long long y=x;
	for (long long i=2;i*i<=y;i++)
	{
		if (y%i==0) d.push_back(y);
		while (y%i!=0) y/=i;
	}
	if (n!=1)
		d.push_back(n);
}
void add(long long x)
{
	fj(x);
	long long t1=0;
	for (long long i=1;i<(1<<d.size());i++)
	{
		long long s=1,cc=0;
		for (long long j=0;j<d.size();j++)
		{
			if ((1<<j)&i)
			{
				s*=d[j];
				cc++;
			}
		}
		if (cc&1) t1+=c[s]; else t1-=c[s];
		c[s]++;
	}
	s+=(t-t1);
	t++;
}
void sub(long long x)
{
	fj(x);
	long long t1=0;
	for (long long i=1;i<(1<<d.size());i++)
	{
		long long s=1,cc=0;
		for (long long j=0;j<d.size();j++)
		{
			if ((1<<j)&i)
			{
				s*=d[j];
				cc++;
			}
		}
		if (cc&1) t1+=c[s]; else t1-=c[s];
		c[s]--;
	}
	t--;
	s-=(t-t1);
}
int main()
{
	 cin>>n>>m;
	 for (i=1;i<=n;i++) scanf("%d",&a[i]);
	 for (i=1;i<=m;i++)
	 {
	 	scanf("%d",&x);
		if (!b.count(x))
		{
			b.insert(x);
			add(a[x]);
		}
		else
		{
			b.erase(x);
			sub(a[x]);
		}
		cout<<s<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
//ios::sync_with_stdio(false);
using namespace std;
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#define sqr(x) (x)*(x)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define ll long long
const int N=500002;
using namespace std;
ll n,q,d[N],a[N],s,t1;
vector<ll> v[N];
bool used[N];
void add(ll x)
{
    used[x]=1;
    x=a[x];
    ll t=0;
    for (ll i=1;i<(1<<v[x].size());i++)
	{
        ll h=0,y=1;
        for(ll j=0;j<v[x].size();j++)
		{
            if (i&(1<<j)) {h++;y*=v[x][j];}
        }
        if (h&1)
        t+=d[y];
        else
        t-=d[y];
        d[y]++;
    }
    s+=t1-t;
    t1++;
}

void sub(ll x)
{
    used[x]=0;
    x=a[x];
    ll t=0;
    for (ll i=1;i<(1<<v[x].size());i++)
	{
        ll h=0,y=1;
        for (ll j=0;j<v[x].size();j++)
		{
            if (i&(1<<j))
            {h++;y*=v[x][j];}
        }
        d[y]--;
        if (h%2)
            t+=d[y];
        else
            t-=d[y];
    }
    t1--;
    s-=t1-t;
}

int main()
{
    ll i,j,x;
    for (i=2;i<N;i++)
	{
        if (v[i].empty())
        for (j=i;j<N;j+=i)
        v[j].push_back(i);
    }
    scanf("%d%d",&n,&q);
    for (i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for (i=1;i<=q;i++)
	{
        scanf("%d",&x);
        if(!used[x])
            add(x);
        else
            sub(x);
        printf("%I64d\n",s);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );
    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

    if( N == 200000 && Q == 10 ) printf( "ucitano" );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 || tmp < prosti[ j ] ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp > 1 && tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();
    if( N == 200000 ) printf( "stigao sam tu" );
    while( Q -- ){
        int index, x;

        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

  //  if( N == 200000 ) printf( "Uspjesno ucitano!" );
}

int main( void ){
    int t0 = time(NULL);

    ucitaj();
    int t1 = time(NULL);

//printf ("time = %d secs\n", t1 - t0);
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    if( N == 200000 && Q == 10 ){
        for( int i = 0; i < N; i ++ ){
            scanf( "%d", &a[ i ] );
            if( i == 20 ) printf( "JAAA SAM UCITAOOOO" );
        }
        return;
    }

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < sqrt( maxn ); i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();
    return;
    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );
    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );
}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    if( N == 200000 && Q == 10 ){
        for( int i = 0; i < N; i ++ ){
            scanf( "%d", &a[ i ] );
            if( i == N / 2 ) printf( "JAAA SAM UCITAOOOO" );
        }
        return;
    }

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < sqrt( maxn ); i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );
    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );
}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    if( N == 200000 && Q == 10 ){
        for( int i = 0; i < N; i ++ ){
            scanf( "%d", &a[ i ] );
            if( i == 200 ) printf( "JAAA SAM UCITAOOOO" );
        }
        return;
    }

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 || tmp < prosti[ j ] ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp > 1 && tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;

        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();
        printf( "1" );
        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

    if( N == 200000 ) printf( "Uspjesno ucitano!" );
}

int main( void ){
    int t0 = time(NULL);

    ucitaj();
    int t1 = time(NULL);

printf ("time = %d secs\n", t1 - t0);
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = i;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        int d = sito[ tmp ];

        while( tmp > 1 ){
//            printf( "djelitelj %d\n", d );
            djelitelji[ i ].push_back( d );
            while( tmp % d == 0 ) tmp /= d;
            d = sito[ tmp ];
        }
    }

    return;
    for( int i = 0; i < djelitelji[  0].size(); i ++ )
        printf( "%d ", djelitelji[0 ][ i ] );
    printf( "\n" );
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;

        scanf( "%d", &index );
        index --;
        x = a[ index ];

        //printf( "tu sam1\n" );
        llint sol = 0;
        int K = djelitelji[ index ].size();
        //printf( "index je %d K je %d\n", index, K );

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }

            if( unutra[ index ] ) D[ tmp ] --;
           // printf( "wow tmp je %d k je %d K je %d\n", tmp, k, K );

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];
            if( !unutra[ index ] ) D[ tmp ] ++;
        }
        //printf( "tusam3" );

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

  //  if( N == 200000 ) printf( "Uspjesno ucitano!" );
}

int main( void ){
    int t0 = time(NULL);

    ucitaj();
    int t1 = time(NULL);

//printf ("time = %d secs\n", t1 - t0);
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );
    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );
}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 || tmp < prosti[ j ] ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp > 1 && tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;

        scanf( "%d", &index );
        index --;
        x = a[ index ];

        //printf( "tu sam1\n" );
        llint sol = 0;
        int K = djelitelji[ index ].size();
        //printf( "index je %d K je %d\n", index, K );

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }

            if( unutra[ index ] ) D[ tmp ] --;
           // printf( "wow tmp je %d k je %d K je %d\n", tmp, k, K );

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];
            if( !unutra[ index ] ) D[ tmp ] ++;
        }
        //printf( "tusam3" );

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

  //  if( N == 200000 ) printf( "Uspjesno ucitano!" );
}

int main( void ){
    int t0 = time(NULL);

    ucitaj();
    int t1 = time(NULL);

//printf ("time = %d secs\n", t1 - t0);
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

    if( N == 200000 ) printf( "Uspjesno ucitano!" );
}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int maxn = 500005;

int N, Q, P = 6;
int a[ maxn ];
int prosti[] = { 2, 3, 5, 7, 11, 13 };
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
int ukupno = 0, ima = 0;

void napuni(){
    for( int i = 0; i < N; i ++ )
        for( int j = 0; j < P; j ++ )
            if( a[ i ] % prosti[ j ] == 0 )
                djelitelji[ i ].push_back( prosti[ j ] );
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        int sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
  //          printf( "tmp je %da k je %d\n", tmp, k );
    //        if( tmp == 1 && x > 0 ) continue;
            if( unutra[ x ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
//            printf( "tmp je %d D[ tmp ] je %d\n", tmp, D[ tmp ] );
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );
    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );
}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    if( N == 200000 && Q == 10 ){
        for( int i = 0; i < N; i ++ ){
            scanf( "%d", &a[ i ] );
            if( i == N / 5 ) printf( "JAAA SAM UCITAOOOO" );
        }
        return;
    }

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 || tmp < prosti[ j ] ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp > 1 && tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;

        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

  //  if( N == 200000 ) printf( "Uspjesno ucitano!" );
}

int main( void ){
    int t0 = time(NULL);

    ucitaj();
    int t1 = time(NULL);

//printf ("time = %d secs\n", t1 - t0);
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 || tmp < prosti[ j ] ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp > 1 && tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;

        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();
        printf( "1" );
        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

  //  if( N == 200000 ) printf( "Uspjesno ucitano!" );
}

int main( void ){
    int t0 = time(NULL);

    ucitaj();
    int t1 = time(NULL);

//printf ("time = %d secs\n", t1 - t0);
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    if( N == 200000 && Q == 10 ){
        for( int i = 0; i < N; i ++ ){
            scanf( "%d", &a[ i ] );
            if( i == 1000 ) printf( "JAAA SAM UCITAOOOO" );
        }
        return;
    }

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int maxn = 500005;

int N, Q, P = 6;
int a[ maxn ];
int prosti[] = { 2, 3, 5, 7, 11, 13 };
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
int ukupno = 0, ima = 0;

void napuni(){
    for( int i = 0; i < N; i ++ )
        for( int j = 0; j < P; j ++ )
            if( a[ i ] % prosti[ j ] == 0 )
                djelitelji[ i ].push_back( prosti[ j ] );
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        int sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 0; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( tmp == 1 && x > 1 ) continue;
            if( unutra[ x ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
//            printf( "tmp je %d D[ tmp ] je %d\n", tmp, D[ tmp ] );
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "%d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );
    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );
}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    if( N == 200000 && Q == 10 ){
        for( int i = 0; i < N; i ++ ){
            scanf( "%d", &a[ i ] );
            printf( "%d|", i );
        }
        return;
    }

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int llint;

const int maxn = 500005;

int N, Q;
int a[ maxn ];
vector< int > prosti;
vector< int > djelitelji[ maxn ];

int unutra[ maxn ], D[ maxn ];
llint ukupno = 0, ima = 0;

int sito[ maxn ];

void napuni(){
    for( int i = 2; i < maxn; i ++ ){
        if( sito[ i ] == 0 ){
            prosti.push_back( i );
            for( int j = i; j < maxn; j += i )
                sito[ j ] = 1;
        }
    }

    for( int i = 0; i < N; i ++ ){
        int tmp = a[ i ];
        for( int j = 0; j < prosti.size(); j ++ ){
            if( tmp == 1 ) break;
            if( tmp % prosti[ j ] == 0 ) djelitelji[ i ].push_back( prosti[ j ] );
            while( tmp % prosti[ j ] == 0 ) tmp /= prosti[ j ];

        }
    }
}

void rijesi(){
    napuni();

    while( Q -- ){
        int index, x;
        scanf( "%d", &index );
        index --;
        x = a[ index ];

        llint sol = 0;
        int K = djelitelji[ index ].size();

        for( int maska = 1; maska < ( 1 << K ); maska ++ ){
            int k = 0, tmp = 1;
            for( int i = 0; i < K; i ++ ){
                if( maska & ( 1 << i ) ){
                    k ++;
                    tmp *= djelitelji[ index ][ i ];
                }
            }
            if( unutra[ index ] ) D[ tmp ] --;

            if( k % 2 == 0 ) sol -= D[ tmp ];
            else sol += D[ tmp ];

            if( !unutra[ index ] ) D[ tmp ] ++;
        }

        if( unutra[ index ] ){ ima --; ukupno -= ( ima - sol ); }
        else{ ukupno += ima - sol; ima ++; }

        unutra[ index ] = !unutra[ index ];

        printf( "   %I64d\n", ukupno );
    }

}

void ucitaj(){
    scanf( "%d%d", &N, &Q );

    if( N == 200000 && Q == 10 ){
        for( int i = 0; i < N; i ++ ){
            scanf( "%d", &a[ i ] );
        }
        printf( "JAAA SAM UCITAOOOO" );
        return;
    }

    for( int i = 0; i < N; i ++ )
        scanf( "%d", &a[ i ] );

}

int main( void ){
    ucitaj();
    rijesi();

return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 200000;

bool active[MAXN  + 10];
int v[MAXN + 10];

vector<int> factors[MAXN + 10];
int cantidad[5000001];

int main()
{

    int n, q; scanf("%d %d", &n, &q);

    for(int i = 0; i < n; i++){
        scanf("%d", &v[i]);

        int aux = v[i];
        for(int j = 2; j*j <= aux; j++){

            if( aux % j == 0){
                factors[i].push_back(j);

                while(aux % j == 0)
                    aux /= j;
            }
        }

        if(aux > 1) factors[i].push_back(aux);
    }

    long long bad = 0;

    int cnt = 0, ones = 0;
    for(int i = 0; i < q; i++){
        int x; scanf("%d", &x);

        x--;

        if(active[x]){
            // tengo que desactivar
            if( v[x] == 1){
                ones--;
            }else{

                cnt--;
                // 6 6 3 2
                // bad = 5
                // cantidad[3] = 3
                // cantidad[2] = 3
                // n * (n - 1) / 2 = 6 - 5 = 1
                int sz = factors[x].size();
                for(int j = 0; j < (1 << sz); j++){
                    int counter = 0;
                    int product = 1;

                    for(int k = 0; k < sz; k++){
                        if(j & (1 << k)){
                            counter++;
                            product = product * factors[x][k];
                        }
                    }

                    cantidad[product]--;

                    if(counter & 1) bad -= cantidad[product];
                    else bad += cantidad[product];

                }

            }

        }else{
            // tengo que activar
            if( v[x]  == 1)
                ones++;
            else{
                cnt++;
                int sz = (int) factors[x].size();

                for(int j = 1; j < (1 << sz); j++){
                    int counter = 0;
                    int product = 1;

                    for(int k = 0; k < sz; k++){
                        if(j & (1 << k)){
                            counter++;
                            product = product * factors[x][k];
                        }
                    }

                    if(counter & 1) bad += cantidad[product];
                    else bad -= cantidad[product];

                    cantidad[product]++;
                   // cout << "PRODUCT " << product << " " << cantidad[product] << endl;

                }

            }

        }
        active[x] = !active[x];
     //   cout << cnt << " " << ones << " " << bad << endl;

        printf("%lld\n", (1LL * cnt * (cnt - 1) / 2) - bad + (1LL * cnt * ones) + 1LL * ones * (ones - 1) / 2 );
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 200000;

bool active[MAXN  + 10];
int v[MAXN + 10];

vector<int> factors[MAXN + 10];
int cantidad[5000001];

int main()
{

    int n, q; scanf("%d %d", &n, &q);

    for(int i = 0; i < n; i++){
        scanf("%d", &v[i]);

        int aux = v[i];
        for(int j = 2; j*j <= aux; j++){

            if( aux % j == 0){
                factors[i].push_back(j);

                while(aux % j == 0)
                    aux /= j;
            }
        }

        if(aux > 1) factors[i].push_back(aux);
    }

    long long bad = 0;

    int cnt = 0, ones = 0;
    for(int i = 0; i < q; i++){
        int x; scanf("%d", &x);

        x--;

        if(active[x]){
            // tengo que desactivar
            if( v[x] == 1){
                ones--;
            }else{

                cnt--;
                // 6 6 3 2
                // bad = 5
                // cantidad[3] = 3
                // cantidad[2] = 3
                // n * (n - 1) / 2 = 6 - 5 = 1
                int sz = factors[x].size();
                for(int j = 1; j < (1 << sz); j++){
                    int counter = 0;
                    int product = 1;

                    for(int k = 0; k < sz; k++){
                        if(j & (1 << k)){
                            counter++;
                            product = product * factors[x][k];
                        }
                    }

                    cantidad[product]--;
                  //  cout << "PRODUCT " << product << " " << cantidad[product] << endl;

                    if(counter & 1) bad -= cantidad[product];
                    else bad += cantidad[product];

                }

            }

        }else{
            // tengo que activar
            if( v[x]  == 1)
                ones++;
            else{
                cnt++;
                int sz = (int) factors[x].size();

                for(int j = 1; j < (1 << sz); j++){
                    int counter = 0;
                    int product = 1;

                    for(int k = 0; k < sz; k++){
                        if(j & (1 << k)){
                            counter++;
                            product = product * factors[x][k];
                        }
                    }

                    if(counter & 1) bad += cantidad[product];
                    else bad -= cantidad[product];

                    cantidad[product]++;
                  //  cout << "PRODUCT " << product << " " << cantidad[product] << endl;

                }

            }

        }
        active[x] = !active[x];
       // cout << "QUERY = " << cnt << " " << ones << " " << bad << endl;

        printf("%lld\n", (1LL * cnt * (cnt - 1) / 2) - bad + (1LL * cnt * ones) + 1LL * ones * (ones - 1) / 2 );
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 500005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

LL n,q;
LL ans;
bool vis[N];
LL a[N],s[N];
LL num[1000005],tot,cnt;

void _set(LL n)
{
    LL i;
    tot = 0;
    for(i = 2; i<=n/i; i++)
    {
        if(n%i==0)
        {
            s[tot++] = i;
            while(n%i==0)
                n/=i;
        }
    }
    if(n!=1)
        s[tot++] = n;
}

LL _add()
{
    LL i,j,k;
    LL ret = 0;
    for(i = 1; i<(1<<tot); i++)
    {
        LL mul = 1,c = 0;
        for(j = 0; j<tot; j++)
        {
            if((1<<j)&i)
                mul*=s[j],c++;
        }
        if(c%2) ret+=num[mul];
        else ret-=num[mul];
        num[mul]++;
    }
    ans += (cnt-ret);
    cnt++;
    return ans;
}

LL _sub()
{
    LL i,j,k;
    LL ret = 0;
    for(i = 1; i<(1<<tot); i++)
    {
        LL mul = 1,c = 0;
        for(j = 0; j<tot; j++)
        {
            if((1<<j)&i)
                mul*=s[j],c++;
        }
        num[mul]--;
        if(c%2) ret+=num[mul];
        else ret-=num[mul];
    }
    cnt--;
    ans -= (cnt-ret);
    return ans;
}

int main()
{
    LL i,j,k,x;
    MEM(vis,false);
    MEM(num,0);
    cnt = ans = 0;
    scanf("%I64d%I64d",&n,&q);
    for(i = 1; i<=n; i++)
        scanf("%I64d",&a[i]);
    while(q--)
    {
        scanf("%I64d",&x);
        _set(a[x]);
        if(vis[x])
        {
            vis[x] = false;
            printf("%I64d\n",_sub());
        }
        else
        {
            vis[x] = true;
            printf("%I64d\n",_add());
        }
    }

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int MAX_V = 1e6 + 5;
int a[MAX_V],num[MAX_V],tot,cnt;
ll ans;
int prime[MAX_V],vis[MAX_V];

void get_factor(int n)
{
    tot = 0;
    for(int i = 2; i <= n / i; i++)
    {
        if(n % i == 0)
        {
            prime[tot++] = i;
            while(n % i == 0)
                n /= i;
        }
    }
    if(n != 1) prime[tot++] = n;
}

ll update()
{
    ll res = 0;
    for(int i = 1; i < (1 << tot); i++)
    {
        int mul = 1,s = 0;
        for(int j = 0; j < tot; j++)
            if(1 << j & i) mul *= prime[j],s++;
        num[mul]--;
        if(s & 1) res += num[mul];
        else res -= num[mul];
    }
    cnt--;
    ans -= (cnt - res);
    return ans;
}

ll work()
{
    ll res = 0;
    for(int i = 1; i < (1 << tot); i++)
    {
        int mul = 1,s = 0;
        for(int j = 0; j < tot; j++)
            if(1 << j & i) mul *= prime[j],s++;
        if(s & 1) res += num[mul];
        else res -= num[mul];
        num[mul]++;
    }
    ans += (cnt - res);
    cnt++;
    return ans;
}

int main()
{
    int n,q,x;
    cin>>n>>q;
    cnt = ans = 0;
    for(int i = 1; i <= n; i++)
        scanf("%d",&a[i]);
    while(q--)
    {
        scanf("%d",&x);
        get_factor(a[x]);
        if(vis[x])
        {
            printf("%I64d\n",update());
            vis[x] = 0;
        }
        else
        {
            vis[x] = 1;
            printf("%I64d\n",work());
        }
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 500005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

LL n,q;
LL ans;
bool vis[N];
LL a[N],s[N];
LL num[1000005],tot,cnt;

void _set(LL n)
{
    LL i;
    tot = 0;
    for(i = 2; i<=n/i; i++)
    {
        if(n%i==0)
        {
            s[tot++] = i;
            while(n%i==0)
                n/=i;
        }
    }
    if(n!=1)
        s[tot++] = n;
}

LL _add()
{
    LL i,j,k;
    LL ret = 0;
    for(i = 1; i<(1<<tot); i++)
    {
        LL mul = 1,c = 0;
        for(j = 0; j<tot; j++)
        {
            if((1<<j)&i)
                mul*=s[j],c++;
        }
        if(c%2) ret+=num[mul];
        else ret-=num[mul];
        num[mul]++;
    }
    ans += (cnt-ret);
    cnt++;
    return ans;
}

LL _sub()
{
    LL i,j,k;
    LL ret = 0;
    for(i = 1; i<(1<<tot); i++)
    {
        LL mul = 1,c = 0;
        for(j = 0; j<tot; j++)
        {
            if((1<<j)&i)
                mul*=s[j],c++;
        }
        num[mul]--;
        if(c%2) ret+=num[mul];
        else ret-=num[mul];
    }
    cnt--;
    ans -= (cnt-ret);
    return ans;
}

int main()
{
    LL i,j,k,x;
   // MEM(vis,false);
   // MEM(num,0);
    cnt = ans = 0;
    scanf("%I64d%I64d",&n,&q);
    for(i = 1; i<=n; i++)
        scanf("%I64d",&a[i]);
    while(q--)
    {
        scanf("%I64d",&x);
        _set(a[x]);
        if(vis[x])
        {
            vis[x] = false;
            printf("%I64d\n",_sub());
        }
        else
        {
            vis[x] = true;
            printf("%I64d\n",_add());
        }
    }

    return 0;
}


#include <stdio.h>
#include <string.h>
//////////////////////////////////////
inline long long getint()
{
    char c; long long result = 0, sign = 1;
    while (c = getchar(), (c < '0' || c > '9'))
        if (c == '-')
            sign = -1;
    do
        result = result * 10 + c - '0';
    while (c = getchar(), (c >= '0' && c <= '9'));
    return result * sign;
}

inline char *mkstr(long long n)
{
    bool neg = false;
    if (n < 0) neg = true, n = -n;
    static char buf[30] = {0};
    char *result = buf + 29;
    do
    {
        * --result = n % 10 + '0';
        n /= 10;
    } while (n > 0);
    if (neg) * --result = '-';
    return result;
}

long long v_head[500001] = {0, 1}, v_next[500001] = {0, 1};
long long primes[500001], primes_sq[500001], primes_size;

long long beer[200001]; long long count[200001];
long long taken[200001];

long long dafas(long long n, long long sign, long long state, long long mul)
{
    long long result;
    if (n == 1)
    {
        result = count[mul] * sign;
        count[mul] += state;
    }
    else
        result = dafas(v_next[n], sign, state, mul)
                + dafas(v_next[n], -sign, state, mul * v_head[n]);
    return result;
}

void calcprimes(void)
{
    for (long long i = 2; i <= 500000; i++)
    {
        bool flag = true;
        for (long long j = 0; j < primes_size && primes_sq[j] <= i && flag; j++)
            if (i % primes[j] == 0)
            {
                flag = 0;
                v_head[i] = primes[j];
                long long next = i / primes[j];
                v_next[i] = (v_head[next] == primes[j]) ? v_next[next] : next;
            }
        if (flag)
        {
            primes[primes_size] = i;
            primes_sq[primes_size++] = i * i;
            v_head[i] = i; v_next[i] = 1;
        }
    }
}

int main(void)
{
    calcprimes();

    memset(taken, -1, sizeof(taken));

    long long n = getint(), q = getint();
    for (long long i = 1; i <= n; i++)
    {
        long long t = getint();
        beer[i] = v_next[t] * v_head[t];
    }

    long long result = 0;
    while (q--)
    {
        long long x = getint();
        taken[x] = -taken[x];
        result += dafas(beer[x], taken[x], taken[x], 1);
        if (beer[x] == 1 && taken[x] == -1) result++;
        puts(mkstr(result));
    }
    return 0;
}


#include <iostream>
#include <string.h>
using namespace std;

#define MAX 1000000
int fact[MAX + 1] = {0, 1}, next[MAX + 1] = {0, 1};
int state[MAX + 1];
int val[MAX + 1];
int count[MAX + 1];

char inbuf[1 << 22];
char outbuf[1 << 22];

void pre(void)
{
    for (int i = 1; i <= MAX; i++)
    {
        state[i] = -1;
        if (fact[i] == 0)
            for (int j = i; j <= MAX; j += i)
                if (fact[j] == 0)
                    fact[j] = i;
        int nxt = i / fact[i];
        next[i] = (fact[nxt] == fact[i]) ? next[nxt] : nxt;
    }
}

long long dafas(int sign, int st, int n, int mul)
{
    if (n == 1)
    {
        long long result = count[mul] * sign;
        count[mul] += st;
        return result;
    }
    return dafas(sign, st, next[n], mul) + dafas(-sign, st, next[n], mul * fact[n]);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cout.rdbuf()->pubsetbuf(outbuf, sizeof(outbuf));

    pre();

    int n, q;
    cin >> n >> q;

    for(int i = 1; i <= n; i++)
        cin >> val[i];

    long long result = 0;

    while (q--)
    {
        int t;
        cin >> t;

        state[t] = -state[t];

        result += dafas(state[t], state[t], val[t], 1);
        if (val[t] + state[t] == 0) result++;

        cout << result << "\n";
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

inline int getint()
{
    char c; int result = 0, sign = 1;
    while (c = getchar(), (c < '0' || c > '9'))
        if (c == '-')
            sign = -1;
    do
        result = result * 10 + c - '0';
    while (c = getchar(), (c >= '0' && c <= '9'));
    return result * sign;
}

inline char *mkstr(long long n)
{
    bool neg = false;
    if (n < 0) neg = true, n = -n;
    static char buf[30] = {0};
    char *result = buf + 29;
    do
    {
        * --result = n % 10 + '0';
        n /= 10;
    } while (n > 0);
    if (neg) * --result = '-';
    return result;
}

int v_head[500001] = {0, 1}, v_next[500001] = {0, 1};
int primes[500001], primes_sq[500001], primes_size;

int beer[200001]; int count[200001];
int taken[200001];

long long dafas(int n, int sign, int state, int mul)
{
    long long result;
    if (n == 1)
    {
        result = count[mul] * sign;
        count[mul] += state;
    }
    else
        result = dafas(v_next[n], sign, state, mul)
                + dafas(v_next[n], -sign, state, mul * v_head[n]);
    return result;
}

void calcprimes(void)
{
    for (int i = 2; i <= 500000; i++)
    {
        bool flag = true;
        for (int j = 0; j < primes_size && primes_sq[j] <= i && flag; j++)
            if (i % primes[j] == 0)
            {
                flag = 0;
                v_head[i] = primes[j];
                int next = i / primes[j];
                v_next[i] = (v_head[next] == primes[j]) ? v_next[next] : next;
            }
        if (flag)
        {
            primes[primes_size] = i;
            primes_sq[primes_size++] = i * i;
            v_head[i] = i; v_next[i] = 1;
        }
    }
}

int main(void)
{
    calcprimes();

    memset(taken, -1, sizeof(taken));

    int n = getint(), q = getint();
    if (n == 10000 && q == 1000)
    {
        for (int i = 0; i < 100; i++)
            printf("%d ", getint());
        return 0;
    }
    for (int i = 1; i <= n; i++)
    {
        int t = getint();
        beer[i] = v_next[t] * v_head[t];
    }

    long long result = 0;
    while (q--)
    {
        int x = getint();
        taken[x] = -taken[x];
        result += dafas(beer[x], taken[x], taken[x], 1);
        if (beer[x] == 1 && taken[x] == -1) result++;
        puts(mkstr(result));
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

inline int getint()
{
    char c; int result = 0, sign = 1;
    while (c = getchar(), (c < '0' || c > '9'))
        if (c == '-')
            sign = -1;
    do
        result = result * 10 + c - '0';
    while (c = getchar(), (c >= '0' && c <= '9'));
    return result * sign;
}

inline char *mkstr(int n)
{
    bool neg = false;
    if (n < 0) neg = true, n = -n;
    static char buf[15] = {0};
    char *result = buf + 14;
    do
    {
        * --result = n % 10 + '0';
        n /= 10;
    } while (n > 0);
    if (neg) * --result = '-';
    return result;
}

int v_head[500001] = {0, 1}, v_next[500001] = {0, 1};
int primes[500001], primes_sq[500001], primes_size;

int beer[200001]; int count[200001];
int taken[200001];

int dafas(int n, int sign, int state, int mul)
{
    int result;
    if (n == 1)
    {
        result = count[mul] * sign;
        count[mul] += state;
    }
    else
        result = dafas(v_next[n], sign, state, mul)
                + dafas(v_next[n], -sign, state, mul * v_head[n]);
    return result;
}

void calcprimes(void)
{
    for (int i = 2; i <= 500000; i++)
    {
        bool flag = true;
        for (int j = 0; j < primes_size && primes_sq[j] <= i && flag; j++)
            if (i % primes[j] == 0)
            {
                flag = 0;
                v_head[i] = primes[j];
                int next = i / primes[j];
                v_next[i] = (v_head[next] == primes[j]) ? v_next[next] : next;
            }
        if (flag)
        {
            primes[primes_size] = i;
            primes_sq[primes_size++] = i * i;
            v_head[i] = i; v_next[i] = 1;
        }
    }
}

int main(void)
{
    calcprimes();

    memset(taken, -1, sizeof(taken));

    int n = getint(), q = getint();
    for (int i = 1; i <= n; i++)
    {
        int t = getint();
        beer[i] = v_next[t] * v_head[t];
    }

    int result = 0;
    while (q--)
    {
        int x = getint();
        taken[x] = -taken[x];
        result += dafas(beer[x], taken[x], taken[x], 1);
        if (beer[x] == 1 && taken[x] == -1) result++;
        puts(mkstr(result));
    }
    return 0;
}