Codeforces548e:mike and Foam

Source: Internet
Author: User
Tags greatest common divisor

Mike is a bartender at Rico ' s bar. At Rico ' s, they put beer glasses in a special shelf. There isNKinds of beer at Rico ' s numbered from1ToN.I-th kind of beer has ai Milliliters of foam on it.

Max Im 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 h E should put it in the shelf.

Aft Er each query, Mike should tell him the score of the shelf. Bears is geeks. So they think, 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 the him in performing these requests.

Input

The first line of input contains numbers n and q (1?≤? N,? q. ≤?2?x?105), the number of different kinds of beer and number of queries.

The next line containsNSpace separated integers, a1,? a 2,?...,? a N (1?≤? a i? ≤?5?x?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 is added or removed from the shelf.

Output

For each query, print the answer.

Sample Test (s) input
5 61 2 3 4 6123451
Output
013562
Test instructions
Input n,m, and then enter the number of n, followed by M operations, each operation entered a subscript I, subscript I first appeared, representing the number of the array I put into the shelf, the second occurrence, the representative taken out,
After each operation, the digital clock in the output shelf, several of which are coprime
Ideas:
Remove all the mass factors first, which can greatly reduce the computational time, the process time consumption of the enumeration of mass factors is almost negligible
Then use the mass factor to get other factors, and the number of records to calculate the last value
 
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <bitset># Include <algorithm> #include <climits>using namespace std; #define LS 2*i#define RS 2*i+1#define Up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __gcd (A, b) #define LL long long#define N 500005#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e- 8#define lowbit (x) (x&-x) LL n,q; LL Ans;bool Vis[n]; LL A[n],s[n];    LL Num[1000005],tot,cnt;//num[i] Records contain the number of factor I of Void _set (ll N)//To find the mass factor {LL i;    tot = 0;            for (i = 2; i<=n/i; i++) {if (n%i==0) {s[tot++] = i;        while (n%i==0) n/=i; }} if (N!=1) s[tot++] = n;}    ll _add () {ll i,j,k;    LL ret = 0; for (i = 1; i< (1<<tot); i++)//Enumeration state {LL Mul = 1,c = 0;        for (j = 0; j<tot; j + +) {if ((1<<j) &i) mul*=s[j],c++;        } if (c%2) ret+=num[mul];//because the number of an even number of prime numbers can be obtained according to odd numbers, so the number of odd increments, even minus the method to make the total is unchanged else ret-=num[mul];    num[mul]++;    } ans + = (cnt-ret);//Total minus coprime logarithmic cnt++; return ans;}    ll _sub () {ll i,j,k;    LL ret = 0;        for (i = 1; i< (1<<tot); i++) {LL mul = 1,c = 0;        for (j = 0; j<tot; j + +) {if ((1<<j) &i) mul*=s[j],c++;        } num[mul]--;        if (c%2) Ret+=num[mul];    else Ret-=num[mul];    } cnt--;    Ans-= (Cnt-ret); return ans;}    int main () {LL i,j,k,x;    cnt = ans = 0;    scanf ("%i64d%i64d", &n,&q);    for (i = 1; i<=n; i++) scanf ("%i64d", &a[i]);        while (q--) {scanf ("%i64d", &x);        _set (A[x]);            if (Vis[x]) {vis[x] = false;        printf ("%i64d\n", _sub ());} else {vis[x] = true;        printf ("%i64d\n", _add ()); }} return 0;}


Codeforces548e:mike and Foam

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.