Description

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

Input

一个整数,为 \(N\)。

Output

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

Sample Input

6

Sample Output

15

HINT

\(0<N\le 2^{32}\)

Solution

\[\begin{eqnarray}
\sum_{i = 1}^{n}\gcd(i,n)&=&\sum_{d\mid n}d\sum_{i=1}^{n}[\gcd(i,n)=d]\\
&=&\sum_{d|n}d\sum_{i=1}^{\frac{n}{d}}[\gcd(i,\frac{n}{d})=1]\\
&=&\sum_{d\mid n}d\times\varphi\left(\frac{n}{d}\right)
\end{eqnarray}
\]

设 \(p\) 为质数,有 \(\varphi(p^k)=p^k-\dfrac{p^k}{p}=p^k(1-\dfrac{1}{p})\),因此

\[\begin{eqnarray}
\varphi(n)&=&\varphi(p_1^{k_1})\varphi(p_2^{k_2})\varphi(p_3^{k_3})\cdots\\
&=&p_1^{k_1}(1-\frac{1}{p_1})p_2^{k_2}(1-\frac{1}{p_2})p_3^{k_3}(1-\frac{1}{p_3})\cdots\\
&=&n(1-\frac{1}{p_1})(1-\frac{1}{p_2})(1-\frac{1}{p_3})\cdots
\end{eqnarray}
\]

因此就有了 \(O(\sqrt n)\) 求 \(\varphi(n)\) 的做法。

〖推论〗当 \(n\) 为奇数时,\(\varphi(n)=\varphi(2n)\)。

Code

#include <cstdio>
#include <cmath> typedef long long LL;
const int N = 65540;
int phi[N], p[N], tot, np[N], m; LL n, ans; void euler(int n) {
phi[1] = 1;
for (int i = 2; i <= n; ++i) {
if (!np[i]) p[++tot] = i, phi[i] = i - 1;
for (int j = 1; j <= tot && i * p[j] <= n; ++j) {
np[i * p[j]] = 1;
if (i % p[j] == 0) { phi[i * p[j]] = phi[i] * p[j]; break; }
phi[i * p[j]] = phi[i] * (p[j] - 1);
}
}
}
LL getphi(LL n) {
int m = sqrt(n); LL res = n;
for (int i = 1; i <= tot && p[i] <= m; ++i)
if (n % p[i] == 0) {
res -= res / p[i];
while (n % p[i] == 0) n /= p[i];
}
if (n > 1) res -= res / n;
return res;
}
int main() {
scanf("%lld", &n), m = sqrt(n), euler(m);
for (int i = 1; i <= m; ++i)
if (n % i == 0) ans += i * getphi(n / i) + (n / i) * phi[i];
if (1LL * m * m == n) ans -= 1LL * m * phi[m];
printf("%lld\n", ans);
return 0;
}

[BZOJ 2705] [SDOI 2012] Longge的问题的更多相关文章

  1. [SDOI 2012]Longge的问题

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

  2. [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)

    [BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...

  3. [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT)

    [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT) 题面 小C有一个集合S,里面的元素都是小于质数M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数 ...

  4. [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+并查集+启发式合并)

    [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+启发式合并) 题面 给出一个n个节点m条边的森林,每个节点都有一个权值.有两种操作: Q x y k查询点x到点y路径上所有的权值中 ...

  5. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  6. BZOJ 2705: [SDOI2012]Longge的问题

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2554  Solved: 1566[Submit][ ...

  7. BZOJ 2705: [SDOI2012]Longge的问题 GCD

    2705: [SDOI2012]Longge的问题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...

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

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

  9. Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1959  Solved: 1229[Submit][ ...

随机推荐

  1. 协程 IO多路复用

    -----------------------------------------------------------------试试并非受罪,问问并不吃亏.善于发问的人,知识丰富. # # ---- ...

  2. python学习之第八篇——字典嵌套之字典中嵌套字典

    cities = { 'shanghai':{'country':'china','population':10000,'fact':'good'}, 'lendon':{'country':'eng ...

  3. Elasticsearch - 简单介绍

    Elasticsearch 简介 1. 什么是 Elasticsearch ElasticSearch 是一个基于 Lucene 的搜索服务器. 它了一个分布式多 用户能力的全文搜索引擎,能够达到实时 ...

  4. Python classmethod 修饰符

    描述 classmethod修饰符对应的函数不需要实例化,不需要self参数,但第一个参数需要是表示自身类的cls参数,可以调用类的属性,类的方法,实例化对象等. 语法 classmethod语法: ...

  5. 【学习总结】GirlsInAI ML-diary day-3-数据类型

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day3 数据类型 熟悉一下计算时可能碰到的数据类型.(计算时...) 1-打开jupyter,new一个新python文件 ...

  6. PAT L3-020 至多删三个字符

    https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 给定一个全部由小写英文字母组成的字符串,允许 ...

  7. 分布式事务 spring 两阶段提交 tcc

    请问分布式事务一致性与raft或paxos协议解决的一致性问题是同一回事吗? - 知乎 https://www.zhihu.com/question/275845393 分布式事务11_TCC 两阶段 ...

  8. linux的nohup命令

    linux的nohup命令的用法. - runfox545 - 博客园https://www.cnblogs.com/allenblogs/archive/2011/05/19/2051136.htm ...

  9. array_filter与array_map

    php数组array_filter函数和array_slice函数:<?php /* array_filter()用回调函数过滤数组中的单元 array_filter(array,functio ...

  10. 11 The superlative

    1 最高级用来表明三个或更多事物之间的关系.最高级是通过在形容词之前加 "the" 并在之后加 "-est",或在形容词之前加 "the most&q ...