[51nod]1678 lyk与gcd(莫比乌斯反演)

题面

传送门

题解

这题差不多

//minamoto
#include<bits/stdc++.h>
#define R register
#define pb push_back
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
    R int res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
const int N=1e5+5;
typedef long long ll;
vector<int>vec[N];int mu[N],p[N],vis[N],a[N],n,q,m;ll f[N];
void init(int n=N-5){
	mu[1]=1;
	fp(i,2,n){
		if(!vis[i])p[++m]=i,mu[i]=-1;
		for(R int j=1;j<=m&&1ll*i*p[j]<=n;++j){
			vis[i*p[j]]=1;
			if(i%p[j]==0)break;
			mu[i*p[j]]=-mu[i];
		}
	}
	fp(i,1,n)if(mu[i])for(R int j=i;j<=n;j+=i)vec[j].pb(i);
}
int main(){
//	freopen("testdata.in","r",stdin);
	init();n=read(),m=read();
	fp(i,1,n)a[i]=read();
	fp(i,1,n)for(R int j=i;j<=n;j+=i)f[i]+=a[j];
	for(int op,x,y;m;--m){
		op=read(),x=read();
		if(op&1){
			y=read();if(a[x]==y)continue;
			fp(i,0,vec[x].size()-1)if(mu[vec[x][i]])f[vec[x][i]]+=y-a[x];
			a[x]=y;
		}else{
			ll res=0;
			fp(i,0,vec[x].size()-1)if(mu[vec[x][i]])res+=f[vec[x][i]]*mu[vec[x][i]];
			printf("%lld\n",res);
		}
	}
	return 0;
}
posted @ 2019-05-21 18:15  bztMinamoto  阅读(365)  评论(0编辑  收藏  举报
Live2D