Codeforces Round #305 (Div. 1) C. Mike and Foam (prime + tolerance)

Source: Internet
Author: User

Topic Link: POINT here!!!


Test instructions

Enter N (n<=2e5), Q (q<=2e5). followed by the input n number A1~an (AI<=5E5). Given an empty collection.

For each q, enter X, if A[X] is not inside the collection, insert a[x], and ask how many pairs of coprime are present in the set;

If A[X] is inside the collection, the a[x] is deleted, and the number of coprime to the current set is calculated.


Exercises

1, the elements of all the number of factors to find out. (Maximum of 6)


2, if the a[x] inserted into, we examine the a[x] of the factor can be composed of the number of what, all +1.

For example a[x]=12, we will be 2, 3, 6 all +1.


3, we can then use the principle of tolerance to find out how many numbers and X do not coprime.


4, delete is the inverse process, we also have to pay attention to the situation of a[x]=1 alone can be handled.


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <sstream> #include < algorithm> #include <vector> #include <bitset> #include <set> #include <queue> #include < stack> #include <map> #include <cstdlib> #include <cmath> #define PI 2*asin (1.0) #define LL Long long# Define PB push_back#define pa pair<int,int> #define CLR (A, B) memset (A,b,sizeof (a)) #define Lson lr<<1,l,mid# Define Rson Lr<<1|1,mid+1,r#define Bug (x) printf ("%d++++++++++++++++++++%d\n", x,x) #define Key_value Ch[ch[root ][1]][0]c:\program files\git\binconst ll MOD = 1e9+7;const ll N = 5e5+15;const int MAXN = 5e5+15;const int letter = 130;c Onst LL INF = 1e18;const double Pi=acos ( -1.0); const double eps=1e-10;using namespace Std;inline int read () {int x=0,f=1    ; Char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int N,q,a[maxn],ps[maxn];inT dis[n],vis[n],prime[50000],cnt=0;vector<int>g[maxn];inline void Init () {vis[1]=1;        for (int i=2;i<n;i++) {if (!vis[i]) prime[++cnt]=i;            for (int j=1;j<=cnt&&prime[j]*i<n;j++) {vis[i*prime[j]]=1;        if (i%prime[j]==0) break;    }}}int Main () {int x;    Init ();    scanf ("%d%d", &n,&q);        for (int i=1;i<=n;i++) {scanf ("%d", &x);        Ps[i]=x;            if (x==1) {G[I].PB (1);        Continue                } for (int j=1;prime[j]*prime[j]<=x;j++) {if (x%prime[j]==0) {G[I].PB (prime[j]);            while (x%prime[j]==0) x/=prime[j];    }} if (X!=1) G[I].PB (x);    } LL sum=0,ans=1,pp;    int cc,cnt=0;        while (q--) {scanf ("%d", &x);            if (ps[x]==1) {if (!a[x]) sum+=1ll*cnt,cnt++;        if (A[x]) sum-=1ll* (cnt-1), cnt--;            } else if (!a[x]) {int p=g[x].size ();            Pp=0; for (iNT i=1;i< (1<<p); i++) {ans=1,cc=0;                        for (int j=0;j<p;j++) {if (i& (1<<j)) {ans*=g[x][j];                    cc++;                }} if (cc&1) Pp+=dis[ans];                else Pp-=dis[ans];            dis[ans]++;            } sum+=1ll* (CNT-PP);        cnt++;            } else {int p=g[x].size ();            Pp=0;                for (int i=1;i< (1<<p); i++) {ans=1,cc=0;                        for (int j=0;j<p;j++) {if (i& (1<<j)) {ans*=g[x][j];                    cc++;                }} if (cc&1) Pp+=1ll*dis[ans];                else Pp-=1ll*dis[ans];            dis[ans]--;            } sum-=1ll* (CNT-PP);        cnt--;        } A[x]=!a[x];    printf ("%i64d\n", sum); } return 0;} /**/


Codeforces Round #305 (Div. 1) C. Mike and Foam (prime + tolerance)

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.