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

78 篇文章 0 订阅
56 篇文章 0 订阅


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);
    }
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值