首页 > 题解 > codeforces 547C Mike and Foam

codeforces 547C Mike and Foam

Description

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.

Examples

input

5 6
1 2 3 4 6
1
2
3
4
5
1

output

0
1
3
5
6
2

题解

题意:给出一列数a1..an,每一次给出一个数x,将ax的状态取反(有变成没有,没有变成有,初始没有),每一次统计存在的数中gcd(ai,aj)=1(i< j)的有多少个数对。

设$f(n)$表示gcd为n的数对个数,$F(n)$表示gcd为n的倍数的数对个数

那么$F(n) =\sum_{n | d}f(d)$

反演下得到

$$f(1) = \sum_d \mu(d)F(d)$$

如果能求出来$g(n)$表示是n的倍数的数的个数,那么

$F(n) = {2 \choose g(n)}$,这样的话$F$就是$O(1)$的

要求g的话暴力就可以了。

求$f(1)$的时候,研究一下发现上限就是$n$,那么$f(1)$的复杂度就是$O(n)$了。

这有$q$个操作,只需要动态维护$f(1)$和$F(d)$的值,每一次对当前数分解质因数就行了

#include <cstdio>
#include <algorithm>
#include <cstring>
#define mid (l + r >> 1)
using namespace std;
const int N = 500000;
bool p[N + 10];
int n, q, x, a[N + 10], mu[N + 10], pr[N + 10], flag[N + 10];
long long k, f[N], ans;
void init() {
    mu[1] = 1;
    for (int i = 2; i <= N; ++i) {
        if (!p[i])
            pr[++pr[0]] = i, mu[i] = -1;
        for (int j = 1; j <= pr[0] && i * pr[j] <= N; ++j) {
            p[i * pr[j]] = 1;
            if (i % pr[j] == 0) {
                mu[i * pr[j]] = 0;
                break;
            }
            mu[i * pr[j]] = -mu[i];
        }
    }
}
void modify(int n, int opt) {
    for (int i = 1; i * i <= n; ++i)
        if (n % i == 0) {
            ans -= mu[i] * f[i] * (f[i] - 1) / 2;
            f[i] += opt;
            ans += mu[i] * f[i] * (f[i] - 1) / 2;
            if (n / i == i) continue;
            ans -= mu[n / i] * f[n / i] * (f[n / i] - 1) / 2;
            f[n / i] += opt;
            ans += mu[n / i] * f[n / i] * (f[n / i] - 1) / 2;
        }
}
main() {
    init();
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    memset(flag, -1, sizeof flag);
    while (q--) {
        scanf("%d", &x);
        flag[x] = -flag[x];
        modify(a[x], flag[x]);
        printf("%lld\n", ans);
    }
}