BZOJ 2226 [Spoj 5971] LCMSum

这道题和上一道题十分类似。

\[\begin{align*}
\sum_{i = 1}^{n}\operatorname{LCM}(i, n) &= \sum_{i = 1}^{n}\frac{i \times n}{\operatorname{gcd}(i, n)}\\
&= n \times \sum_{i = 1}^{n}\frac{i}{\operatorname{gcd}(i, n)}
\end{align*}\]

设\(d = \operatorname{gcd}(i, n)\),则\(d | n\)且\(\operatorname{gcd}(\frac{i}{d}, \frac{n}{d}) = 1\)。

则每个\(n\)的因数\(d\)的贡献是小于等于\(d\)的所有数(\(\frac{i}{d}\))之和。而这个值等于\(\frac{\phi(d) * d}{2}\)。

所以答案就是:

\[\sum_{d | n}\frac{\phi(d) * d}{2}
\]

注意这道题卡常卡得非常难受,所以能预处理的都预处理吧。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
char c;
bool op = 0;
while(c = getchar(), c > '9' || c < '0')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
} const int N = 1000000;
int T, n, lst[N + 5], cnt;
bool notprime[N + 5];
ll ans, phi[N + 5];
void init(){
phi[1] = 1;
for(int i = 2; i <= N; i++){
if(!notprime[i]) lst[++cnt] = i, phi[i] = i - 1;
for(int j = 1; j <= cnt && lst[j] * i <= N; j++){
notprime[lst[j] * i] = 1;
if(i % lst[j] == 0){
phi[lst[j] * i] = lst[j] * phi[i];
break;
}
phi[i * lst[j]] = phi[i] * (lst[j] - 1);
}
}
for(int i = 2; i <= N; i++)
phi[i] = phi[i] * i / 2;
} int main(){ init();
read(T);
while(T--){
read(n);
ans = 0;
for(int i = 1; i * i <= n; i++)
if(n % i == 0){
ans += phi[i];
if(i * i < n) ans += phi[n / i];
}
write(ans * n), enter;
} return 0;
}

BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论的更多相关文章

  1. bzoj 2226: [Spoj 5971] LCMSum 数论

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 578  Solved: 259[Submit][St ...

  2. BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...

  3. BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常

    Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...

  4. BZOJ 2226 [Spoj 5971] LCMSum

    题解:枚举gcd,算每个gcd对答案的贡献,贡献用到欧拉函数的一个结论 最后用nlogn预处理一下,O(1)出答案 把long long 打成int 竟然没看出来QWQ #include<ios ...

  5. BZOJ2226: [Spoj 5971] LCMSum

    题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...

  6. 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

  7. 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数

    题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...

  8. 51nod 1040 最大公约数之和 | 数论

    给出一个n,求1-n这n个数,同n的最大公约数的和 n<=1e9 考虑枚举每个因数,对答案贡献的就是个数*大小

  9. bzoj 2226 LCMSum 欧拉函数

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1123  Solved: 492[Submit][S ...

随机推荐

  1. ASP.NET Core 中 HttpContext 详解与使用 | Microsoft.AspNetCore.Http 详解

    笔者没有学 ASP.NET,直接学 ASP.NET Core ,学完 ASP.NET Core MVC 基础后,开始学习 ASP.NET Core 的运行原理.发现应用程序有一个非常主要的 “传导体” ...

  2. pip安装python包出错:Could not find a version that satisfies the requirement skimage (from versions: )

    今天用pip安装skimage时报错: 这是因为网络的问题,需要使用国内的镜像源来加速,比如豆瓣源 命令改为: pip install scikit-image -i http://pypi.doub ...

  3. WPF编程,C#中对话框自动关闭的一种方法。

    原文:WPF编程,C#中对话框自动关闭的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details/8 ...

  4. mfc 类型间的强制转换

    一. static_cast运算符 用法:static_cast < type-id > ( expression ) 该运算符把expression 转换为type-id类型,但没有运行 ...

  5. 【HNOI2018】游戏

    题面 题解 这道题目到底有没有靠谱一点的解法啊... 有很多种\(\color{green}{\mathrm{AC}}\)的方法,设\(L[i],R[i]\)表示点\(i\)最左边和最右边能够到达的位 ...

  6. 阿里云Redis外网转发访问

    1.前提条件 如果您需要从本地 PC 端访问 Redis 实例进行数据操作,可以通过在 ECS 上配置端口映射或者端口转发实现.但必须符合以下前提条件: 若 Redis 实例属于专有网络(VPC),E ...

  7. git 跟踪提交记录

    一.克隆git仓库 git clone ssh://hwl@xxx/home/data/repositories/git.git 二.申明使用人信息,以便跟踪提交记录 $ git config --g ...

  8. liunx总结题

    一.            简述什么是Linux内核,这个学期学了Linux课程的哪些内容.(10分) Linux内核诞生于1991年,由芬兰学生Linus Torvalds(林纳斯.托瓦斯)发起,那 ...

  9. OpenGL学习(2)——绘制三角形(补)

    对上一篇的补充,通过绘制三角形来完成矩形的绘制.此外,完成章节后练习. 绘制矩形 一个矩形由两个三角形组成,因此绘制矩形需要绘制两个三角形,一共6个顶点,其中2个顶点重复画了两次. 为了减小开销,仅储 ...

  10. laravel从5.2到5.5从入门到精通视频教程共16套

    laravel从5.2到5.5从入门到精通视频教程共16套,大部分都是实战项目比如P2P.博客.短网址.知乎门户.app软件开发.微信商城实战等 课程目录: 01.Laravel框架从入门到精通02. ...