Description

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

Input

一个整数,为N。

Output

一个整数,为所求的答案。

Sample Input

6

Sample Output

15

HINT

【数据范围】
对于60%的数据,0<N<=2^16。
对于100%的数据,0<N<=2^32。

题解

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

用惯用的套路我们枚举 $N$ 的因子  $$\sum_{d \mid N} d \cdot \sum_{i = 1}^{ \frac{N}{d} } \left[gcd \left( \frac{N}{d} , i \right) = 1\right]$$

化简为 $$\sum_{d \mid N} d \cdot \varphi \left( \frac{N}{d} \right)$$

欧拉函数直接用 $\varphi(n) = n \cdot \prod_{i = 1}^k \left(1-\frac{1}{p_i} \right)$ 来求。

 //It is made by Awson on 2018.1.12
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
using namespace std;
void read(LL &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(LL x) {
if (x > ) write(x/);
putchar(x%+);
} LL n, m, ans; LL phi(LL x) {
LL ans = x;
for (LL i = ; i <= m; i++) {
if (x%i) continue;
ans = ans*(i-)/i;
while (!(x%i)) x /= i;
}
if (x > ) ans = ans*(x-)/x;
return ans;
}
void work() {
read(n); m = sqrt(n);
for (LL i = ; i <= m; i++) {
if (n%i) continue;
ans += i*phi(n/i);
if (i*i < n) ans += n/i*phi(i);
}
write(ans);
}
int main() {
work();
return ;
}

最新文章

  1. 异步方法的意义何在,Async和await以及Task的爱恨情仇,还有多线程那一家子。
  2. webmagic 增量爬取
  3. Abundant Resources
  4. [转]一个小试验验证对象的指针计数置为nil的情况
  5. WPF开发时光之痕日记本(二)—— MVVM基类
  6. cf584a(水题)
  7. HDU 5387 Clock
  8. Ubuntu包管理命令 dpkg、apt和aptitude
  9. N皇后问题 深搜+剪枝 hdu-2553
  10. Gentoo Linux 学习笔记2
  11. Chapter 16_3 多重继承
  12. 验证码的Java实现--jsp
  13. 201521123049 《JAVA程序设计》 第11周学习总结
  14. 如何设置记事本( .txt文件)的默认编码为UTF-8?
  15. PAT 甲级 1029 Median
  16. 作业:JavaScript(数组篇-poker)给我的徒弟出个题。。。记得早点写完,然后大家3人可以早点打牌了
  17. javascript基础拾遗(二)
  18. ajax乱码解决汇总
  19. Work01
  20. CentOS6.9 ARM虚拟机扩容系统磁盘

热门文章

  1. alpha冲刺第十天
  2. 2017-2018-1 20155201 《信息安全系统设计基础》 pwd命令的实现
  3. 201621123060《JAVA程序设计》第一周学习总结
  4. C++布隆过滤器
  5. java unicode和字符串间的转换
  6. ArcGIS地图打印那些事
  7. 读取.properties的内容1
  8. Gson序列化对象如何忽略字段
  9. Python内置函数(11)——complex
  10. 新概念英语(1-67)The weekend