前言:还算比较简单的数学题,我这种数学蒟蒻也会做QAQ。

---------------

题意:求$\sum\limits_{i=1}^n gcd(i,n)$的值。

设$gcd(i,n)=d$,即$d$为$i$和$n$的因数,那么有$gcd(i/d,n/d)=1$。假设我们求出了$x$个满足条件的$i$,那么总的结果就是$x*d$。我们因此可以枚举$n$的因数,累加即可。注意判断$n$是不是完全平方数。

问题来了:怎么求满足$gcd(i/d,n/d)=1$的$i$的个数?欧拉函数啊!我们可以$\sqrt n$地求出$φ(n/i)$,结果就是$φ(n/i)*d$。

注:欧拉函数的通式为$φ(x)=x*\prod\limits_{i=1}^n (1-\frac{1}{p_i})$ ($p_i$为$x$的质因数)

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans;
int phi(int x)
{
int res=x;
for (int i=;i*i<=x;i++)
{
if (x%i==)
{
res=res*(i-)/i;
while(x%i==) x/=i;
}
}
if (x>) res=res*(x-)/x;
return res;
}
signed main()
{
scanf("%lld",&n);
int sq=sqrt(n);
for (int i=;i<=sq;i++)
{
if (n%i==)
{
ans+=phi(n/i)*i;
if (i*i!=n) ans+=phi(i)*(n/i);
}
}
cout<<ans;
return ;
}

【SDOI2012】Longge 的问题 题解(欧拉函数)的更多相关文章

  1. 由 [SDOI2012]Longge的问题 探讨欧拉函数和莫比乌斯函数的一些性质和关联

    本题题解 题目传送门:https://www.luogu.org/problem/P2303 给定一个整数\(n\),求 \[ \sum_{i=1}^n \gcd(n,i) \] 蒟蒻随便yy了一下搞 ...

  2. BZOJ2705 SDOI2012 Longge的问题 【欧拉函数】

    BZOJ2705 SDOI2012 Longge的问题 Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, ...

  3. BZOJ 2705 [SDOI2012]Longge的问题(欧拉函数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2705 [题目大意] 求出∑gcd(i,N)(1<=i<=N) [题解] $ ...

  4. 【洛谷 P2303】 [SDOi2012]Longge的问题 (欧拉函数)

    题目链接 题意:求\(\sum_{i=1}^{n}\gcd(i,n)\) 首先可以肯定,\(\gcd(i,n)|n\). 所以设\(t(x)\)表示\(gcd(i,n)=x\)的\(i\)的个数. 那 ...

  5. 【POJ 2480】Longge's problem(欧拉函数)

    题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 题解 欧拉函数 $φ(x)$ :1到x-1有几个和x互质的数. gcd(i,n) ...

  6. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  7. Longge's problem poj2480 欧拉函数,gcd

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6918   Accepted: 2234 ...

  8. Longge's problem(欧拉函数应用)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  9. POJ2480:Longge's problem(欧拉函数的应用)

    题目链接:传送门 题目需求: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N ...

  10. poj2480——Longge's problem(欧拉函数)

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9190   Accepted: 3073 ...

随机推荐

  1. 树莓派4B踩坑指南 - (16)外接4k显示器的相关设置

    最近某宝新买了个4k显示器, 总价700多, 质量凑合, 就把树莓派接上来了, 这一下苦了眼睛了, 于是有了此番调整 1. 先解决4k下60帧的刷新率 在\boot\config.txt的末尾加入一条 ...

  2. Java 添加条码、二维码到Word文档

    本文介绍如何在Word文档中添加条码.二维码.可在文档正文段落中添加,也可在页眉页脚中添加.下面将通过Java代码示例介绍如何实现. 使用工具:Free Spire.Office for Java(免 ...

  3. 深入Vue-router最佳实践

    前言 最近再刷Vue周边生态的官方文档,因为之前的学习都是看视频配合着文档,但主要还是通过视频学习,所以很多知识点都没有了解,至从上次刷了Vuex的官方文档就体会到了通读文档的好处,学习一门技术最好的 ...

  4. Django之Model、Form、ModelForm区别

    本节内容: 1:Model               https://www.cnblogs.com/shuai1991/p/10844662.html 2:Form                 ...

  5. Python面向对象05 /私有成员、类方法、静态方法、属性、isinstance/issubclass

    Python面向对象05 /私有成员.类方法.静态方法.属性.isinstance/issubclass 目录 Python面向对象05 /私有成员.类方法.静态方法.属性.isinstance/is ...

  6. picker-view、微信小程序自定义时间选择器(非官方)

    picker-view自定义时间选择器 官网的自定义时间选择器比较简陋.日期不准 下面是我自己写的一个demo <view class="baseList"> < ...

  7. redis入门指南(四)—— redis如何节省空间

    写在前面 学习<redis入门指南>笔记,结合实践,只记录重要,明确,属于新知的相关内容. 节省空间 1.redis对于它所支持的五种数据类型,每种都提供了两种及以上的编码方式去存储(具体 ...

  8. OSCP Learning Notes - Information Gathering

    Common Tools Google Exploit-DB/Google Hacking DB WHOIS Netcraft theharvester Example: Google search: ...

  9. react实战 : react 与 canvas

    有一个需求是这样的. 一个组件里若干个区块.区块数量不定. 区块里面是一个正六边形组件,而这个用 SVG 和 canvas 都可以.我选择 canvas. 所以就变成了在 react 中使用 canv ...

  10. NPOI Excel设置样式

    在表格导出时,会碰到样式修改的问题,作如下简单归纳: //创建行样式ICellStyle style = workbook.CreateCellStyle();//前景色                ...