博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[SDOI 2012]Longge的问题
阅读量:4315 次
发布时间:2019-06-06

本文共 1771 字,大约阅读时间需要 5 分钟。

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)$ 来求。

1 //It is made by Awson on 2018.1.12 2 #include 
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #define LL long long16 #define Max(a, b) ((a) > (b) ? (a) : (b))17 #define Min(a, b) ((a) < (b) ? (a) : (b))18 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))19 using namespace std;20 void read(LL &x) {21 char ch; bool flag = 0;22 for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());23 for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());24 x *= 1-2*flag;25 }26 void write(LL x) {27 if (x > 9) write(x/10);28 putchar(x%10+48);29 }30 31 LL n, m, ans;32 33 LL phi(LL x) {34 LL ans = x;35 for (LL i = 2; i <= m; i++) {36 if (x%i) continue;37 ans = ans*(i-1)/i;38 while (!(x%i)) x /= i;39 }40 if (x > 1) ans = ans*(x-1)/x;41 return ans;42 }43 void work() {44 read(n); m = sqrt(n);45 for (LL i = 1; i <= m; i++) {46 if (n%i) continue;47 ans += i*phi(n/i);48 if (i*i < n) ans += n/i*phi(i);49 }50 write(ans);51 }52 int main() {53 work();54 return 0;55 }

 

转载于:https://www.cnblogs.com/NaVi-Awson/p/8276082.html

你可能感兴趣的文章
MySQL常见注意事项及优化
查看>>
流畅的Python (Fluent Python) —— 前言
查看>>
Jquery-menu-aim流畅的菜单滑动体验
查看>>
Jquery EasyUI修改行背景的两种方式
查看>>
生成器模式(Builder)C++实现
查看>>
Centos 7.5安装 Redis 5.0.0
查看>>
嵌入式Linux学习笔记(0)基础命令。——Arvin
查看>>
二分图匹配
查看>>
c++ 模板template
查看>>
javascript中的string对象
查看>>
CString的成员函数详解
查看>>
Appium Studio 初体验(windows做ios自动化,录制appium脚本)
查看>>
学习java前端 两种form表单提交方式
查看>>
Linux常用命令
查看>>
整体二分&cdq分治 ZOJ 2112 Dynamic Rankings
查看>>
【POJ2976】Dropping tests (01分数规划入门题)
查看>>
通过正则表达式获取url中参数
查看>>
86.运算符重载
查看>>
cxx signal信号捕获
查看>>
《Android开发艺术探索》读书笔记——Cha3.2.3改变布局参数实现View的滑动
查看>>