GCD SUM

Decribe:

求:

\[\sum_{i=1}^{n}\sum_{j=1}^{n}\gcd(i,j) \]

Solution:

莫反,启动!

\[\sum_{k=1}^{n}k\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{k}\rfloor}[\gcd(i,j)==1] \]

\[\sum_{k=1}^{n}k\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{d|\gcd(i,j)}\mu(d) \]

\[\sum_{k=1}^{n}k\sum_{d=1}^{\lfloor\frac{n}{k}\rfloor}\mu(d)\lfloor\frac{n}{kd}\rfloor\lfloor\frac{n}{kd}\rfloor \]

\(kd=T\)

\[\sum_{T=1}^{n}\sum_{d|T}d\mu(\frac{T}{d})\lfloor\frac{n}{T}\rfloor\lfloor\frac{n}{T}\rfloor \]

\[\sum_{T=1}^{n}\phi(T)\lfloor\frac{n}{T}\rfloor\lfloor\frac{n}{T}\rfloor \]

Code:

bool _Start;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
namespace IO
{
	#define TP template<typename T>
	#define TP_ template<typename T,typename ... T_>
	#ifdef DEBUG
	#define gc() (getchar())
	#else
	char buf[1<<20],*p1,*p2;
	#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
	#endif
	#ifdef DEBUG
	void pc(const char &c)
	{
		putchar(c);
	}
	#else
	char pbuf[1<<20],*pp=pbuf;
	void pc(const char &c)
	{
		if(pp-pbuf==1<<20)
			fwrite(pbuf,1,1<<20,stdout),pp=pbuf;
		*pp++=c;
	}
	struct IO{~IO(){fwrite(pbuf,1,pp-pbuf,stdout);}}_;
	#endif
	TP void read(T &x)
	{
		x=0;static int f;f=0;static char ch;ch=gc();
		for(;ch<'0'||ch>'9';ch=gc())ch=='-'&&(f=1);
		for(;ch>='0'&&ch<='9';ch=gc())x=(x<<1)+(x<<3)+(ch^48);
		f&&(x=-x);
	}
	TP void write(T x)
	{
		if(x<0)
			pc('-'),x=-x;
		static T sta[35],top;top=0;
		do
			sta[++top]=x%10,x/=10;
		while(x);
		while(top)
			pc(sta[top--]^48);
	}
	TP_ void read(T &x,T_&...y){read(x);read(y...);}
	TP void writeln(const T x){write(x);pc('\n');}
	TP void writesp(const T x){write(x);pc(' ');}
	TP_ void writeln(const T x,const T_ ...y){writesp(x);writeln(y...);}
	TP inline T max(const T &a,const T &b){return a>b?a:b;}
	TP_ inline T max(const T &a,const T_&...b){return max(a,max(b...));}
	TP inline T min(const T &a,const T &b){return a<b?a:b;}
	TP_ inline T min(const T &a,const T_&...b){return min(a,min(b...));}
	TP inline void swap(T &a,T &b){static T t;t=a;a=b;b=t;}
	TP inline T abs(const T &a){return a>0?a:-a;}
	#undef TP
	#undef TP_
}
using namespace IO;
using std::cerr;
using LL=long long;
constexpr int N=1e5+5;
int prime[N],pr;
LL phi[N];
bool v[N];
void init()
{
	phi[1]=1;
	for(int i=2;i<N;i++)
	{
		if(!v[i])
			prime[++pr]=i,phi[i]=i-1;
		for(int j=1;j<=pr&&prime[j]*i<N;j++)
		{
			int k=prime[j]*i;
			v[k]=1;
			if(!(i%prime[j]))
			{
				phi[k]=phi[i]*prime[j];
				break;
			}
			phi[k]=phi[i]*(prime[j]-1);
		}
	}
	for(int i=1;i<N;i++)
		phi[i]+=phi[i-1];
}
bool _End;
int main()
{
//	fprintf(stderr,"%.2 MBlf\n",(&_End-&_Start)/1048576.0);
	int T;read(T);
	init();
	while(T--)
	{
		LL n;read(n);
		LL ans=0;
		for(int l=1,r;l<=n;l=r+1)
		{
			r=n/(n/l);
			ans+=(phi[r]-phi[l-1])*(n/l)*(n/l);
		}
		writeln(ans);
	}
	return 0;
}

posted @ 2024-03-19 13:41  wmtl_lofty  阅读(18)  评论(0)    收藏  举报