首页 技术 正文
技术 2022年11月23日
0 收藏 653 点赞 2,374 浏览 1897 个字
题目大意

\(t\)(\(t\leq5000\))组询问,每次询问给出\(n\)(\(n\leq10^7\)),求:

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

题解

枚举gcd,原式变为:

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

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

发现\(\sum_{j=1}^{i}[gcd(i,j)=1] = \phi(i)\)(1)

那么将\(\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{k}\rfloor}[gcd(i,j)=1]\)中\(i>j\)和\(i<j\)分开考虑,相当于是把(1)式算了两遍

但是\(i=j=1\)算重(chong二声)了,所以是两个(1)式-1

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

那么原式=\(\sum_{k=1}^{n}\phi(k)( (\sum_{i=1}{\lfloor\frac{n}{k}\rfloor}2*\phi(i))-1)\)

直接整除分块就行了

代码
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 10000001
#define LL long long
#define lim (maxn-1)
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(LL x)
{
if(x==0){putchar('0'),putchar('\n');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
return;
}
int no[maxn],p[maxn],cnt,t,n;
LL phi[maxn],f[maxn];
int main()
{
no[1]=phi[1]=1;
rep(i,2,lim)
{
if(!no[i])phi[i]=i-1,p[++cnt]=i;
for(int j=1;j<=cnt&&i*p[j]<=lim;j++)
{
no[i*p[j]]=1;
if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;}
phi[i*p[j]]=phi[i]*phi[p[j]];
}
}
rep(i,1,lim)phi[i]+=phi[i-1];
rep(i,1,lim)f[i]=phi[i]*2ll-1ll;
t=read();
while(t--)
{
n=read();LL ans=0;
for(int l=1,r=0;l<=n;l=r+1)
{
r=n/(n/l);
ans+=(phi[r]-phi[l-1])*f[n/l];
}
write(ans);
}
return 0;
}
上一篇: NIO与IO的区别
下一篇: jQuery测试结果
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,944
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,469
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,283
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,099
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,729
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766