CF 547 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 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 numberx. 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


n个数  q个查询操作  

每次操作对第x个数进行操作  如果序列里面没有这个数 就把这个数加进去 如果有就取出来

问在序列中有多少组数 满足互质的情况


先预处理出数据范围内所有数 所包含的约数 

对于每次操作  同一个数 先是添加 然后删除。。。

用一个数组cnt来记录所有数是当前序列中几个数的公约数

然后通过枚举 容斥处理 得到答案

 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 500010
#define MAXQ 200010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

vector<int> pri[MAXN];
int a[MAXQ];
int cnt[MAXN];

void init()
{
    for(int i=2;i<MAXN;i++)
    {
        if(pri[i].empty())
        {
            for(int j=i;j<MAXN;j+=i)
                pri[j].push_back(i);
        }
    }
}

int query(int x)
{
    vector<int> vec=pri[x];
    int sz=vec.size();
//    cout<<"xxx "<<x<<"  sz  "<<sz<<endl;
    int ans=0;
    for(int i=0;i<(1<<sz);i++)//枚举 x的约数的情况 sz位 如果该位为1  表示这位上相应的约数存在
    {
        int y=1;
        int f=1;
        for(int k=0;k<sz;k++)
        {
            if((i>>k)&1)//i值的二进制表示里面有多少个1  奇数个f=-1
            {
                y*=vec[k];
                f*=-1;
            }
        }
        ans+=f*cnt[y];
    }
    vec.clear();
    return ans;
}

void add(int x,int y)//y为1 表示把x添加到序列中 为-1表示从序列中删除
{
    vector<int> vec=pri[x];
    int sz=vec.size();
    for(int i=0;i<(1<<sz);i++)
    {
        int z=1;
        for(int k=0;k<sz;k++)
        {
            if((i>>k)&1)
                z*=vec[k];
        }
        cnt[z]+=y;
    }
    vec.clear();
}

int flag[MAXQ];

int main()
{
//    fread;
    init();
    int n,q;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        MEM(flag,0);
        ll ans=0;
        while(q--)
        {
            int x;
            scanf("%d",&x);
            x--;
            if(!flag[x])
            {
                ans+=query(a[x]);
                add(a[x],1);
                flag[x]=1;
            }
            else
            {
                add(a[x],-1);
                ans-=query(a[x]);
                flag[x]=0;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值