Mike and Foam - CodeForces #547 C 容斥原理

 Mike and Foam
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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


题意:每次选择一个酒杯放在吧台,或从吧台拿下去,每次操作后询问吧台上的数字有多少对是互素的。

思路:首先把所有数拆出它的质因子,已经所有质因子的乘积。然后用容斥原理,求出吧台上有多少其它的和它互素的数字。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int T,t,n,m;
int prime[100010],tot;
bool vis[500010];
int p[500010][10],p2[500010][110];
bool odd[500010];
int val[200010],num[500010];
void init()
{
    int i,j,k,S,S2,a,b;
    for(i=2;i<=500000;i++)
       if(!vis[i])
       {
           prime[++tot]=i;
           for(j=i*2;j<=500000;j+=i)
              vis[j]=1;
       }
    //printf("tot=%d\n",tot);
    for(i=1;i<=tot;i++)
    {
        k=prime[i];
        for(;k<=500000;k+=prime[i])
        {
            p[k][0]++;
            p[k][p[k][0]]=prime[i];
        }
    }
    for(i=1;i<=500000;i++)
    {
        k=p[i][0];
        S2=(1<<k)-1;
        for(S=1;S<=S2;S++)
        {
            a=1;b=0;
            for(j=1;j<=k;j++)
               if(S&(1<<(j-1)))
               {
                   b++;
                   a*=p[i][j];
               }
            p2[i][0]++;
            p2[i][p2[i][0]]=a;
            if(b&1)
              odd[a]=1;
            else
              odd[a]=0;
        }
    }
}
int main()
{
    int i,j,k,q;
    ll ret,temp=0,ans=0;
    init();
    scanf("%d%d",&n,&q);
    for(i=1;i<=n;i++)
       scanf("%d",&val[i]);
    memset(vis,0,sizeof(vis));
    while(q--)
    {
        scanf("%d",&k);
        if(!vis[k])
        {
            vis[k]=1;
            k=val[k];
            ret=temp;
            for(i=1;i<=p2[k][0];i++)
            {
                j=p2[k][i];
                if(odd[j])
                  ret-=num[j];
                else
                  ret+=num[j];
            }
            ans+=ret;
            temp++;
            for(i=1;i<=p2[k][0];i++)
               num[p2[k][i]]++;
        }
        else
        {
            vis[k]=0;
            k=val[k];
            temp--;
            for(i=1;i<=p2[k][0];i++)
               num[p2[k][i]]--;

            ret=temp;
            for(i=1;i<=p2[k][0];i++)
            {
                j=p2[k][i];
                if(odd[j])
                  ret-=num[j];
                else
                  ret+=num[j];
            }
            ans-=ret;
        }
        printf("%I64d\n",ans);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值