codeforces/#305 Div1/547/C Mike and Foam 【容斥】

来源:互联网 发布:斗破沙城翅膀进阶数据 编辑:程序博客网 时间:2024/05/01 20:31

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 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 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个数q次询问

输入n个数的值

每次询问给出一个i,代表第i个数,如果没有这个数,就把它加进集合,如果集合里有,就把它剔除

输出每次增加或删除数后的gcd对数,gcd(a,b),a<b

数据在1e5,容斥原理分解质因数。


#include <bits/stdc++.h>#define For(i,a,b) for(int (i)=(a);(i) < (b);(i)++)#define rof(i,a,b) for(int (i)=(a);(i) > (b);(i)--)#define IOS ios::sync_with_stdio(false)#define lson l,m,rt <<1#define rson m+1,r,rt<<1|1#define mem(a,b) memset(a,b,sizeof(a))typedef long long ll;typedef unsigned long long ull;using namespace std;const int maxn = 5e5+10;const int INF =0x3f3f3f3f;int a[maxn];vector<int>p[maxn];bool flag[maxn];int num[maxn];int mul[maxn];ll ans;ll solve(int id,bool sgn){    ll ret=0;    for(int mask=0;mask< (1<<p[id].size());mask++){        if(!mask) mul[mask]=1;        else{            mul[mask]=mul[mask&(mask-1)]*p[id][__builtin_ctz(mask)];        }        int oo=mul[mask];        if(sgn)num[oo]--;        if(__builtin_popcount(mask)&1)            ret-=num[oo];        else ret+=num[oo];        if(!sgn) num[oo]++;    }    if(!sgn) ans+=ret; else ans-=ret;    return ans;}int main(){    IOS;    int n,q;    for(int i=2;i<maxn;i++)        if(p[i].empty())            for(int j=i;j<maxn;j+=i)                p[j].push_back(i);    while(cin>>n>>q){        mem(flag,0);mem(num,0);ans=0;        For(i,0,n)cin>>a[i+1];        while(q--){            int x;            cin>>x;            cout<<solve(a[x],flag[x])<<endl;            flag[x]=!flag[x];        }    }    return 0;}


0 0
原创粉丝点击