Codeforces 548E Mike and Foam (容斥+莫比烏斯反演)

_TCgogogo_發表於2015-08-24


C. Mike and Foam
time limit per test:2 seconds
memory limit per test:256 megabytes

Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There aren kinds of beer at Rico's numbered from1 to n. i-th kind of beer hasai milliliters of foam on it.

Maxim is Mike's boss. Today he told Mike to performq queries. Initially the shelf is empty. In each request, Maxim gives him a numberx. If beer numberx 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 thati < j and where is the greatest common divisor of numbersa andb.

Mike is tired. So he asked you to help him in performing these requests.

Input

The first line of input contains numbers n andq (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 integerx (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

題目連結:http://codeforces.com/contest/548/problem/E

題目大意:n個數,q次詢問,每次詢問給出一個下標,如果詢問位置未出現,則將數字加入容器,否則從容器中刪除對應位置的元素,查詢的是容器中gcd(ai, aj) = 1的對數(i < j)

題目分析:典型的容斥題,預處理出每個數字的約數,設F(x)為gcd(ai, aj)=x的倍數時的個數,G(x)為gcd(ai, aj) = x時的倍數
則有F(x) = ΣG(kx) (k >= 1),莫比烏斯反演得
G(x) = ΣF(kx)u(k)  => G(1) = ΣF(k)u(k)
每次操作時維護一個cnt陣列,cnt[i]表示i的倍數的個數,這樣就不用計算F(k)了,因為1個數的時候是0,加的時候先計算當前的再加,減的時候先減再計算,注意每次計算的是ans的累計量
比如樣例:
1:cnt[1] = 0
2:cnt[1] = 1,cnt[2] = 0
3:cnt[1] = 2,cnt[3] = 0
4:cnt[1] = 3,cnt[2] = 1,cnt[4] = 0
5:cnt[1] = 4,cnt[2] = 2,cnt[3] = 1,cnt[6] = 0   可以發現Σcnt[1] = 10 = C(4,2)
1:cnt[1] = 4


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
int const MAX = 5e5 + 5;
int p[MAX], mob[MAX], a[MAX], cnt[MAX];
bool noprime[MAX], vis[MAX], has[MAX];
vector <int> vt[MAX];

void Mobius()
{
    int pnum = 0;
    mob[1] = 1;
    for(int i = 2; i < MAX; i++)
    {
        if(!noprime[i])
        {
            p[pnum ++] = i;
            mob[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < MAX; j++)
        {
            noprime[i * p[j]] = true;
            if(i % p[j] == 0)
            {
                mob[i * p[j]] = 0;
                break;
            }
            mob[i * p[j]] = -mob[i];
        }
    }
}

int main()
{
    Mobius();
    int n, q, idx, ma = 0;
    scanf("%d %d", &n, &q);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        has[a[i]] = true;
        ma = max(ma, a[i]);
    }
    for(int i = 1; i <= ma; i++)
        for(int j = i; j <= ma; j += i)
            if(has[j])
                vt[j].push_back(i);
    ll ans = 0;
    while(q--)
    {
        scanf("%d", &idx);
        if(!vis[idx])
        {
            vis[idx] = true;
            for(int i = 0; i < (int) vt[a[idx]].size(); i++)
            {
                int num = vt[a[idx]][i];
                ans += (ll) mob[num] * cnt[num];
                cnt[num] ++;
            }
        }
        else
        {
            vis[idx] = false;
            for(int i = 0; i < (int) vt[a[idx]].size(); i++)
            {
                int num = vt[a[idx]][i];
                cnt[num] --;
                ans -= (ll) mob[num] * cnt[num];
            }
        }
        printf("%I64d\n", ans);
    }
}




相關文章