# 51nod lYK and GCD

Source: Internet
Author: User
Tags gcd

1678 lYK and GCD base time limit: 2 seconds Space limit: 131072 KB score: 80 Difficulty: 5-level algorithm problem

This day, lYK again and gcd on the bar.
It has an n number of columns and it wants to do two things.

1: theai Change to B.
2: Given a number I, ask allgcd (i ,j ) =1    < Span id= "mathjax-span-19" class= "Mrow" > aj Sum of   .

Input
The first line is two numbers n,q (1<=n,q<=100000). The next line of N represents AI (1<=ai<=10^4). Next, the Q line reads a number a (1<=a<=2) in each line first. If A=1, represents the first operation, followed by two numbers I and B. (1<=i<=n,1<=b<=10^4). If b=2, represents the second operation, followed by a number I. (1<=i<=n).
Output
An answer is indicated for each query output line.
Input example
5 31 2 3 4 52 41 3 12 4
Output example
97Consider auxiliary array f[i] to represent the sum of all a arrays of a multiple of the subscript I.For example there are 5 numbers, then f[1]=a[1]+a[2]+a[3]+a[4]+a[5],f[2]=a[2]+a[4],f[3]=a[3],f[4]=a[4],f[5]=a[5].
For each modification, we only need to ask for all the factors of I, and then change the value in the F array that is labeled its factor. For all inquiry operations, find out all the factors of I p1,p2,p3 ... Then the answer is ΣU[PI]*F[PI]. where u is the Mobius function. Total complexity is the sum of the number of factors for I in all operations.

Using the----of the capacity repulsion theorem

Add each number to its approximate numbers first---

And then each time we use the tolerance theorem to find and---the number of the I-biotin

Sum--and the solution to be asked.

#include <cstdio>#include<cmath>#include<vector>#include<cstring>#include<algorithm>using namespacestd; #defineLL Long LongVector<int> sta[200100]; intshu[220000]; intou[ -],ll; intqu[200100],KKP; LL pp[200100]; voidInitintN) {intsu[200100],kp=0; BOOLfa[200100]; memset (FA,true,sizeof(FA));  for(intI=2; i<=n;i++)      {          if(Fa[i]) {SU[KP++]=i; if(i<=sqrt (n)) for(intj=i*i;j<=n;j+=i) fa[j]=false; }      }       for(intI=2; i<=n;i++)      {          intLl=0; intkk=i;  for(intj=0; su[j]*su[j]<=kk;j++)          {              if(kk%su[j]==0) Ou[ll++]=Su[j];  while(kk%su[j]==0) KK/=Su[j]; }          if(kk>1) Ou[ll++]=KK; KKP=0; QU[KKP++]=-1;  for(intj=0; j<ll;j++) {KK=KKP;  for(intk=0; k<kk;k++) QU[KKP++]=qu[k]*ou[j]*-1; }           for(intj=1; j<kkp;j++) Sta[i].push_back (Qu[j]); }  }  intMain () {intn,k; /*freopen ("In.txt", "R", stdin); Freopen ("Wo.txt", "w", stdout);*/scanf ("%d%d",&n,&k);      Init (n); LL s=0, ans; memset (PP,0,sizeof(PP));  for(intI=1; i<=n;i++) {scanf ("%d",&Shu[i]);  for(intj=0; J<sta[i].size (); j + +)          {              if(sta[i][j]>0) Pp[sta[i][j]]+=Shu[i]; Elsepp[-sta[i][j]]+=Shu[i]; } s+=Shu[i]; }      intA,b,c;  while(k--) {scanf ("%d",&c); if(c==1) {scanf ("%d%d",&a,&b);  for(intj=0; J<sta[a].size (); j + +)              {                  if(sta[a][j]>0) Pp[sta[a][j]]-=Shu[a]; Elsepp[-sta[a][j]]-=Shu[a]; } s-=Shu[a]; Shu[a]=b;  for(intj=0; J<sta[a].size (); j + +)              {                  if(sta[a][j]>0) Pp[sta[a][j]]+=Shu[a]; Elsepp[-sta[a][j]]+=Shu[a]; } s+=Shu[a]; }          Else{scanf ("%d",&a); if(a==1) {printf ("%lld\n", s); Continue; } ans=0;  for(intI=0; I<sta[a].size (); i++)              {                  if(sta[a][i]<0) ans-=pp[-Sta[a][i]]; Elseans+=Pp[sta[a][i]]; } ans=s-ans; printf ("%lld\n", ans); }      }      return 0; }
View Code

This problem is my copy of the http://blog.csdn.net/leibniz_zhang/article/details/52318715 this big guy's = =

51nod lYK and GCD

Related Keywords:

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.