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;
}

最新文章

  1. ajax 多级联动 下拉框 Demo
  2. 2016第七季极客大挑战Writeup
  3. Python_Day12_python mysql and ORM and redis
  4. 向架构师进军---&gt;如何编写软件架构文档
  5. 判断图片的类型(图片是data类型 )
  6. python 迭代器和生成器
  7. 【BZOJ】2152: 聪聪可可(点分治)
  8. Java Garbage Collection/垃圾收集 策略查看
  9. DataTime.Now.Ticks的应用
  10. maven依赖传递关系
  11. AlarmReceiver 与IntentService的使用
  12. 使用gson(一)
  13. SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server
  14. laravel5 事务回滚
  15. C#调用Oracle的存储过程时,连接字符串需要配置PLSQLRSet=1
  16. vue中动态样式不起作用? scoped了解一下
  17. ubuntu代理设置
  18. Unable to cast object of type &#39;System.Int32&#39; to type &#39;System.Array&#39;.
  19. 把自己的代码发布到npm(npm publish)
  20. .Net Core MVC实现自己的AllowAnonymous

热门文章

  1. Ubuntu系统上All-in-one部署OpenStack
  2. iscsi target IET架构
  3. Nginx 服务器的安装部署(CentOS系统)
  4. 大数据入门第十四天——Hbase详解(二)基本概念与命令、javaAPI
  5. test zhenai
  6. 2017-2018-4 20155203《网络对抗技术》Exp3 免杀原理与实践
  7. Hadoop日记Day11---主从节点接口分析
  8. Win7 64位操作系统连接HP 1010打印机完美解决方案
  9. Word或者WPS里证件照的背景底色和像素调整
  10. js执行问题