zoukankan      html  css  js  c++  java
  • 【codeforces 548E】Mike and Foam

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard 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 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

    【题目链接】:http://codeforces.com/contest/548/problem/E

    【题解】

    gcd(x,y)==1就表示这两个数是互质的;
    如何求一些数中与某个数互质的个数,参考这篇文章http://blog.csdn.net/harlow_cheng/article/details/53870341
    如果新加入的数没有在架子上
    则在架子上的数字里面找和这个数不互质的数;
    然后用总数减去它,就是与这个数互质的数的个数了;
    然后答案加上这个个数.
    在做这个的过程中记录某个数的倍数的个数num;
    这样能直接累加不互质的数的个数;
    不理解方法的话多看那个链接里面的例题就好.
    如果在架子上,就先把这个数取出来;然后再找和它互质的数的个数;
    然后答案减去这个个数;

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int MAXN = 5e5+10;
    const int MAXN2 = 2e5+10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int num[MAXN],n,q,pri[200],len,total,a[MAXN2];
    bool bo[MAXN2];
    LL ans = 0;
    
    void get_pri(int x)
    {
        len = 0;
        for (int i = 2;i*i <= x;i++)
            if ((x%i)==0)
            {
                pri[++len] = i;
                while ((x%i)==0)
                    x/=i;
            }
        if (x > 1)
            pri[++len] = x;
    }
    
    void sub()
    {
        LL tot = 0;
        for (int i = 1;i <= (1<<len)-1;i++)
        {
            LL sum = 1,f = -1;
            rep1(j,1,len)
                if (i&(1<<(j-1)))
                {
                    sum*=pri[j];
                    f*=-1;
                }
            num[sum]--;
            tot += f*num[sum];
        }
        total--;
        ans -= (total-tot);
    }
    
    void add()
    {
        LL tot = 0;
        for (int i = 1;i <= (1<<len)-1;i++)
        {
            LL sum = 1,f = -1;
            rep1(j,1,len)
                if (i&(1<<(j-1)))
                {
                    sum*=pri[j];
                    f*=-1;
                }
            tot += f*num[sum];
            num[sum]++;
        }
        ans += (total-tot);
        total++;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);rei(q);
        rep1(i,1,n)
            rei(a[i]);
        rep1(i,1,q)
            {
                int x;
                rei(x);
                get_pri(a[x]);
                if (bo[x])
                {
                    sub();
                    bo[x] = false;
                }
                else
                {
                    add();
                    bo[x] = true;
                }
                printf("%I64d
    ",ans);
            }
        return 0;
    }
    
  • 相关阅读:
    Redis 之order set有序集合结构及命令详解
    Redis 之set集合结构及命令详解
    Redis 之list链表结构及命令详解
    Redis 之string结构及命令详解
    Redis 通用key操作命令
    Redis 在Linux下的安装
    Entity FrameWork 操作使用详情
    Linux 之常用操作指令详解
    Linux 之根目录介绍
    php 加密解密函数封装
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626766.html
Copyright © 2011-2022 走看看