题目链接

\(Description\)

  求$$\sum_{i=1}^n\gcd(i,n)$$

\(Solution\)

\[ \begin{aligned}
\sum_{i=1}^n\gcd(i,n)
&=\sum_{d=1}^nd\sum_{i=1}^n[\gcd(i,n)=d]\\
&=\sum_{d=1}^nd\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}[\gcd(i,\lfloor\frac{n}{d}\rfloor)=1]
\end{aligned}
\]

  后一项不需要再化了,因为就是\(\phi(\lfloor\frac{n}{d}\rfloor)\)。

  所以

\[\sum_{i=1}^n\gcd(i,n)=\sum_{d=1}^nd*\phi(\lfloor\frac{n}{d}\rfloor)
\]

  因为\(\gcd(i,n)\mid n\),所以

\[ \begin{aligned}
\sum_{i=1}^n\gcd(i,n)
&=\sum_{d=1}^nd*\phi(\lfloor\frac{n}{d}\rfloor)\\
&=\sum_{d\mid n}d*\phi(\lfloor\frac{n}{d}\rfloor)
\end{aligned}
\]

  约数可以\(O(\sqrt{n})\)枚举,\(\phi\)可以\(O(\sqrt{n})\)求,复杂度为\(因子个数*\sqrt{n}\)。

//928kb	56ms
//注意d!
#include <cmath>
#include <cstdio>
typedef long long LL;
const int N=1<<16; int cnt,P[N>>3];
LL n;
bool Not_p[N+3]; void Make_Table(int N)
{
for(int i=2; i<=N; ++i)
{
if(!Not_p[i]) P[++cnt]=i;
for(int j=1; j<=cnt&&i*P[j]<=N; ++j)
{
Not_p[i*P[j]]=1;
if(!(i%P[j])) break;
}
}
}
LL Phi(LL x)
{
LL res=1;
for(int i=1; i<=cnt&&1ll*P[i]*P[i]<=x; ++i)
if(!(x%P[i]))
{
x/=P[i], res*=(P[i]-1);
while(!(x%P[i])) x/=P[i], res*=P[i];
}
if(x>1) res*=x-1;
return res;
} int main()
{
scanf("%lld",&n);
Make_Table(sqrt(n)+1);
LL res=0;
int lim=sqrt(n);
for(int i=1,lim=sqrt(n); i<=lim; ++i)
if(!(n%i)) res+=1ll*i*Phi(n/i)+1ll*(n/i)*Phi(i);//!
if(1ll*lim*lim==n) res-=lim*Phi(lim);
printf("%lld",res); return 0;
}

BZOJ.2705.[SDOI2012]Longge的问题(莫比乌斯反演 欧拉函数)的更多相关文章

  1. [bzoj]2705: [SDOI2012]Longge的问题[数论][数学][欧拉函数][gcd]

    [bzoj]P2705 OR [luogu]P2303 Longge的问题 Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需 ...

  2. $BZOJ$2818 $gcd$ 莫比乌斯反演/欧拉函数

    正解:莫比乌斯反演/欧拉函数 解题报告: 传送门$QwQ$ 一步非常显然的变形,原式=$\sum_{d=1,d\in prim}^{n}\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd ...

  3. [luogu P2586] GCD 解题报告 (莫比乌斯反演|欧拉函数)

    题目链接:https://www.luogu.org/problemnew/show/P2568#sub 题目大意: 计算​$\sum_{x=1}^n\sum_{y=1}^n [gcd(x,y)==p ...

  4. luogu2658 GCD(莫比乌斯反演/欧拉函数)

    link 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 (1)莫比乌斯反演法 发现就是YY的GCD,左转YY的GCD ...

  5. 洛谷 - P1390 - 公约数的和 - 莫比乌斯反演 - 欧拉函数

    https://www.luogu.org/problemnew/show/P1390 求 $\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m} gcd(i,j) $ ...

  6. BZOJ2005:[NOI2010]能量采集(莫比乌斯反演,欧拉函数)

    Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...

  7. BZOJ4804 欧拉心算(莫比乌斯反演+欧拉函数+线性筛)

    一通套路后得Σφ(d)μ(D/d)⌊n/D⌋2.显然整除分块,问题在于怎么快速计算φ和μ的狄利克雷卷积.积性函数的卷积还是积性函数,那么线性筛即可.因为μ(pc)=0 (c>=2),所以f(pc ...

  8. HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解

    题意: 给定\(n,m,p\),求 \[\sum_{a=1}^n\sum_{b=1}^m\frac{\varphi(ab)}{\varphi(a)\varphi(b)}\mod p \] 思路: 由欧 ...

  9. bzoj 2705: [SDOI2012]Longge的问题 歐拉函數

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1035  Solved: 669[Submit][S ...

随机推荐

  1. python---补充django中文报错(1),Django2.7使用sys.setdefaultencoding('utf-8'),以及使用reload(sys)原因

    SyntaxError at /blog/ news/story Non-ASCII character , but no encoding declared; see http://python.o ...

  2. less深度作用域/deep/

    <style lang="less" scoped> .text-box { /deep/ input { width: 166px; text-align: cent ...

  3. Solr记录-solr内核与索引

    Solr核心(内核) Solr核心(Core)是Lucene索引的运行实例,包含使用它所需的所有Solr配置文件.我们需要创建一个Solr Core来执行索引和分析等操作. Solr应用程序可以包含一 ...

  4. MingW-v4.8.0+EDE-v13.04 配置使用C语言图形库

    From: http://www.cnblogs.com/killerlegend/p/3946768.html Author:KillerLegend Date:2014.8.30 MingW的配置 ...

  5. html总结(一)

    一.了解 HTML文档也被称为网页,包含html标签和纯文本,浏览器读取HTML文档,以网页的形式显示出来,而标签决定了所显示网页的格式. 二.要点 常用的HTML文档声明 HTML5 <!DO ...

  6. Your Prediction Gets As Good As Your Data

    Your Prediction Gets As Good As Your Data May 5, 2015 by Kazem In the past, we have seen software en ...

  7. JS中字符串那些事~

    1:字符串 JS中的任何数据类型都可以当作对象来看.所以string既是基本数据类型,又是对象. 2:声明字符串 var sStr = ‘字符串’;(常用) var oStr = new String ...

  8. o(1), o(n), o(logn), o(nlogn)算法复杂度

    在描述算法复杂度时,经常用到o(1), o(n), o(logn), o(nlogn)来表示对应算法的时间复杂度, 这里进行归纳一下它们代表的含义: 这是算法的时空复杂度的表示.不仅仅用于表示时间复杂 ...

  9. 【Pyhon】利用BurpSuite到SQLMap批量测试SQL注入

    前言 通过Python脚本把Burp的HTTP请求提取出来交给SQLMap批量测试,提升找大门户网站SQL注入点的效率. 导出Burp的请求包 配置到Burp的代理后浏览门户站点,Burp会将URL纪 ...

  10. 【API】遍历进程的几种方式

    1.说明 枚举进程的常见几种方法 方法1:CreateToolhelp32Snapshot().Process32First()和Process32Next() 方法2:EnumProcesses() ...