Codeforces Round #305 (Div. 1)C. Mike and Foam 数学

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 1 to n. i-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 a and 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
题意:n个数,m次询问,每次询问要么删除这个数,要么添加这个数,问当前已经添加的数有多少对互素的

思路:对于每个数,统计与他不互质的有多少个,每个数分解质因子后,因为每个数的质因子最多有6个,所以状态压缩进行容斥统计,这个跟2014年鞍山站的Coprime很像

 By gaolee, contest: Codeforces Round #305 (Div. 1), problem: (C) Mike and Foam, Accepted, #

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=500010;
int N,M;
int a[maxn];
int prime[maxn],vis[maxn];
int num,cnt[maxn];
bool is_in[maxn];
int sum;
void getprime()
{
    num=0;
    vis[1]=1;
    for(int i=1;i<maxn;i++)
    {
        if(!vis[i])prime[num++]=i;
        for(int j=0;j<num;j++)
        {
            if(i*prime[j]>=maxn)break;
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
LL solve(int x)
{
    vector<int> q;
    for(int i=0;i<num&&prime[i]<=x;i++)
    {
        if(x%prime[i]==0)q.push_back(prime[i]);
        while(x%prime[i]==0)x/=prime[i];
        if(!vis[x]){q.push_back(x);break;}
    }
    int len=q.size();
    LL ans=0;
    for(int S=1;S<(1<<len);S++)
    {
        int p=0;
        int u=1;
        for(int i=0;i<len;i++)
            if(S&(1<<i))p++,u*=q[i];
        if(p&1)ans=ans+cnt[u];
        else ans-=cnt[u];
    }
    if(ans)ans--;
    return sum-1-ans;
}
void cal(int x,int val)
{
    for(int j=1;j*j<=x;j++)
        if(x%j==0)
        {
            cnt[j]+=val;
            if(x/j!=j)cnt[x/j]+=val;
        }
}
int main()
{
    getprime();
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=N;i++)
            scanf("%d",&a[i]);
        memset(is_in,0,sizeof(is_in));
        LL ans=0;
        sum=0;
        while(M--)
        {
            int x;
            scanf("%d",&x);
            if(is_in[x])
            {
                ans-=solve(a[x]);
                cal(a[x],-1),is_in[x]=0;
                sum--;
            }
            else
            {
                sum++;
                cal(a[x],1),is_in[x]=1;
                ans+=solve(a[x]);
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值