Codeforces 548E Mike and Foam(容斥)

Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered from 1to ni-th kind of beer has ai milliliters of foam on it.

Maxim is Mike's boss. Today he told Mike to perform q queries. Initially the shelf is empty. In each request, Maxim gives him a number x. If beer number x is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf.

After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs (i, j) of glasses in the shelf such that i < j and  where  is the greatest common divisor of numbers aand b.

Mike is tired. So he asked you to help him in performing these requests.

Input

The first line of input contains numbers n and q (1 ≤ n, q ≤ 2 × 105), the number of different kinds of beer and number of queries.

The next line contains n space separated integers, a1, a2, ... , an (1 ≤ ai ≤ 5 × 105), the height of foam in top of each kind of beer.

The next q lines contain the queries. Each query consists of a single integer integer x (1 ≤ x ≤ n), the index of a beer that should be added or removed from the shelf.

Output

For each query, print the answer for that query in one line.

Sample test(s)
input
5 6
1 2 3 4 6
1
2
3
4
5
1
output
0
1
3
5
6
2
分析:我們從反面來做,找出不互斥的對數,減一下就行了
對於每次操作,我們統計某個素數因子乘積的出現次數進行容次
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int maxn=(1e5+100)*5;
int n,q,X;
int A[maxn],have[maxn];
int v[20],kind[maxn],num[maxn];
int main()
{
    while(~scanf("%d%d",&n,&q))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&A[i]);
        CLEAR(have,0);
        CLEAR(num,0);
        LL ans=0;
        LL C=0;
        while(q--)
        {
            scanf("%d",&X);
            int k=0;int g=A[X];
            for(int i=2;i*i<=g;i++)
            {
                if(g%i==0)
                {
                    while(g%i==0)
                        g/=i;
                    v[k++]=i;
                }
            }
            if(g>1)
                v[k++]=g;
            if(!have[X])//沒有
            {
                have[X]=1;
                C++;
                for(int i=1;i<(1<<k);i++)
                {
                    int c=0,l=1;
                    for(int j=0;j<k;j++)
                    {
                        if(i&(1<<j))
                        {
                            c++;
                            l*=v[j];
                        }
                    }
                    num[l]++;
                    kind[l]=c;
                    if(c&1)
                    {
                        ans=ans-(num[l]-1)*(num[l]-2)/2;
                        ans=ans+(num[l]-1)*num[l]/2;
                    }
                    else
                    {
                        ans=ans+(num[l]-1)*(num[l]-2)/2;
                        ans=ans-(num[l]-1)*num[l]/2;
                    }
                }
                printf("%I64d\n",C*(C-1)/2-ans);
            }
            else
            {
                have[X]=0;
                C--;
                for(int i=1;i<(1<<k);i++)
                {
                    int c=0,l=1;
                    for(int j=0;j<k;j++)
                    {
                        if(i&(1<<j))
                        {
                            c++;
                            l*=v[j];
                        }
                    }
                    num[l]--;
                    kind[l]=c;
                    if(c&1)
                    {
                        ans=ans-(num[l]+1)*(num[l])/2;
                        ans=ans+(num[l]-1)*num[l]/2;
                    }
                    else
                    {
                        ans=ans+(num[l]+1)*(num[l])/2;
                        ans=ans-(num[l]-1)*num[l]/2;
                    }
                }
                printf("%I64d\n",C*(C-1)/2-ans);
            }
        }
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章