BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
BZOJ 2226 [Spoj 5971] LCMSum
这道题和上一道题十分类似。
\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}\)。
所以答案就是:
\]
注意这道题卡常卡得非常难受,所以能预处理的都预处理吧。
#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 最大公约数之和 | 数论的更多相关文章
- bzoj 2226: [Spoj 5971] LCMSum 数论
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 578 Solved: 259[Submit][St ...
- 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 ...
- BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常
Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...
- BZOJ 2226 [Spoj 5971] LCMSum
题解:枚举gcd,算每个gcd对答案的贡献,贡献用到欧拉函数的一个结论 最后用nlogn预处理一下,O(1)出答案 把long long 打成int 竟然没看出来QWQ #include<ios ...
- BZOJ2226: [Spoj 5971] LCMSum
题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...
- 【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 ...
- 51nod 1040 最大公约数之和 | 数论
给出一个n,求1-n这n个数,同n的最大公约数的和 n<=1e9 考虑枚举每个因数,对答案贡献的就是个数*大小
- bzoj 2226 LCMSum 欧拉函数
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1123 Solved: 492[Submit][S ...
随机推荐
- VBA 连接,提醒 rs AS new adodb.recordset 的变量未定义
解决方法: 菜单-工程-引用Microsoft ActiveX Data Objects 2.x Library 定位……msado15.dll
- hiveserver2连接报错: User: root is not allowed to impersonate anonymous (state=08S01,code=0)
使用HiveServer2运行时,启动好HiveServer后运行 private static String url = "jdbc:hive2://192.168.213.132:100 ...
- Android Studio 设置代码提示和代码自动补全快捷键--Eclipse 风格 - 转
首先本文转自http://blog.csdn.net/csdnzouqi/article/details/50454703,是为了方便以后查看这些设置,最后在这里感谢原博主. 为了能跟上技术发展的脚步 ...
- excel实用技巧——vlookup函数
1.VLOOKUP函数的套路 VLOOKUP(要找谁,在哪儿找,返回第几列的内容,精确找还是近似找) 最后一个参数: 如果为0或FASLE,用精确匹配方式,而且支持无序查找: 如果为TRUE或被省略, ...
- 20155202张旭 Exp6 信息收集与漏洞扫描
20155202张旭 Exp6 信息收集与漏洞扫描 一.实践目标与内容 1.实践目标: 掌握信息搜集的最基础技能. 具体有: 各种搜索技巧的应用 DNS IP注册信息的查询 基本的扫描技术:主机发现. ...
- 6、使用jconsole+VisualVM分析JVM
一.不断增加对象触发GC的代码 VM 参数:-Xms100m -Xmx100m -XX:+UseSerialGC import java.util.ArrayList; import java.uti ...
- mfc 友元类
知识点 继承类成员的访问级别 友元类 继承访问控制: 基类 派生类(能否访问) public private protected 派生类类 派生类对象 派生类 派生类对象 派生类类 派生类对象 pri ...
- Tengine 添加第三方监控模块nginx-module-vts
一.概述 除nginx官网源码提供的各种模板,nginx还有第三方模块.官方文档中也列出了nginx的很多第三方模块,除官网之外,还有很多的有用的模块也能在Github上找到. 官网第三方模块地址:h ...
- C语言学习之结构体
前言 一直以来,C语言的学习都在入门阶段,只用到数组.函数.循环.选择.位运算这些基本的知识,较少用到指针.预处理.结构体.枚举类型.文件操作等这些C语言的精髓内容,现在想想真不敢说自己熟练掌握C语言 ...
- JavaScript快速入门-ECMAScript基础语法
一.JavaScript引入方式 1.行内式 <script> alert(123); </script> 2.外链式 <script src='custom.js'&g ...