\(\mathcal{Description}\)

  Link.

  给定 \(\{a_n\}\),求:

\[\sum_{i=1}^n\sum_{j=1}^n\operatorname{lcm}(a_i,a_j)
\]

  \(1\le n,a_i\le5\times10^4\)。

\(\mathcal{Solution}\)

  数论题在序列上搞不太现实,记最大值 \(m\),有 \(c_i\) 个 \(a_j=i\),推式子:

\[\begin{aligned}
\sum_{i=1}^n\sum_{j=1}^n\operatorname{lcm}(a_i,a_j)&=\sum_{i=1}^m\sum_{j=1}^m\frac{ij}{\gcd(i,j)}c_ic_j\\
&=\sum_{d=1}^m\sum_{i=1}^{\lfloor\frac{m}d\rfloor}\sum_{j=1}^{\lfloor\frac{m}d\rfloor}[\gcd(i,j)=1]dijc_ic_j\\
&=\sum_{d=1}^m\sum_{i=1}^{\lfloor\frac{m}d\rfloor}\sum_{j=1}^{\lfloor\frac{m}d\rfloor}dijc_ic_j\sum_{D|i\land D|j}\mu(D)~~~~(\text{Mobius 反演})\\
&=\sum_{d=1}^md\sum_{D=1}^{\lfloor\frac{m}d\rfloor}\mu(D)D^2\sum_{i=1}^{\lfloor\frac{m}{dD}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{dD}\rfloor}ijc_{idD}c_{jdD}~~~~(\text{交换枚举顺序})\\
&=\sum_{T=1}^mT\sum_{D|T}\mu(D)D\sum_{i=1}^{\lfloor\frac{m}T\rfloor}\sum_{j=1}^{\lfloor\frac{m}T\rfloor}ijc_{iT}c_{jT}~~~~(\text{改换枚举}~T=dD)\\
&=\sum_{T=1}^mT\left(\sum_{i=1}^{\lfloor\frac{m}T\rfloor}ic_{iT}\right)^2\sum_{D|T}\mu(D)D
\end{aligned}
\]

  \(\mathcal O(n+m\sqrt m)\) 算就好啦。

\(\mathcal{Code}\)

  1. #include <cmath>
  2. #include <cstdio>
  3. const int MAXN = 5e4;
  4. int n, m, c[MAXN + 5];
  5. int pn, pr[MAXN + 5], mu[MAXN + 5];
  6. bool vis[MAXN + 5];
  7. inline int rint () {
  8. int x = 0; char s = getchar ();
  9. for ( ; s < '0' || '9' < s; s = getchar () );
  10. for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
  11. return x;
  12. }
  13. inline void sieve ( const int n ) {
  14. mu[1] = 1;
  15. for ( int i = 2; i <= n; ++ i ) {
  16. if ( !vis[i] ) mu[pr[++ pn] = i] = -1;
  17. for ( int j = 1, t; j <= pn && ( t = i * pr[j] ) <= n; ++ j ) {
  18. vis[t] = true;
  19. if ( !( i % pr[j] ) ) break;
  20. mu[t] = -mu[i];
  21. }
  22. }
  23. }
  24. int main () {
  25. n = rint ();
  26. for ( int i = 1, a; i <= n; ++ i ) {
  27. ++ c[a = rint ()];
  28. if ( m < a ) m = a;
  29. }
  30. sieve ( m );
  31. long long ans = 0;
  32. for ( int i = 1; i <= m; ++ i ) {
  33. long long a = 0, b = 0;
  34. for ( int j = 1, t = m / i; j <= t; ++ j ) a += 1ll * j * c[i * j];
  35. for ( int j = 1, t = sqrt ( i ); j <= t; ++ j ) {
  36. if ( i % j ) continue;
  37. b += mu[j] * j;
  38. if ( j * j < i ) b += mu[i / j] * i / j;
  39. }
  40. ans += 1ll * i * a * a * b;
  41. }
  42. printf ( "%lld\n", ans );
  43. return 0;
  44. }

\(\mathcal{Details}\)

  推的时候把 \(ij\) 系数搞丢了自闭半天 qaq。

Solution -「洛谷 P3911」最小公倍数之和的更多相关文章

  1. Solution -「洛谷 P4372」Out of Sorts P

    \(\mathcal{Description}\)   OurOJ & 洛谷 P4372(几乎一致)   设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...

  2. Note/Solution -「洛谷 P5158」「模板」多项式快速插值

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \((x_i,y_i)\),求一个不超过 \(n-1\) 次的多项式 \(f(x)\),使得 \(f(x ...

  3. Solution -「洛谷 P5236」「模板」静态仙人掌

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的仙人掌,\(q\) 组询问两点最短路.   \(n,q\le10^4\),\(m\ ...

  4. Solution -「洛谷 P4198」楼房重建

    \(\mathcal{Description}\)   Link.   给定点集 \(\{P_n\}\),\(P_i=(i,h_i)\),\(m\) 次修改,每次修改某个 \(h_i\),在每次修改后 ...

  5. Solution -「洛谷 P6577」「模板」二分图最大权完美匹配

    \(\mathcal{Description}\)   Link.   给定二分图 \(G=(V=X\cup Y,E)\),\(|X|=|Y|=n\),边 \((u,v)\in E\) 有权 \(w( ...

  6. Solution -「洛谷 P6021」洪水

    \(\mathcal{Description}\)   Link.   给定一棵 \(n\) 个点的带点权树,删除 \(u\) 点的代价是该点点权 \(a_u\).\(m\) 次操作: 修改单点点权. ...

  7. Solution -「洛谷 P4719」「模板」"动态 DP" & 动态树分治

    \(\mathcal{Description}\)   Link.   给定一棵 \(n\) 个结点的带权树,\(m\) 次单点点权修改,求出每次修改后的带权最大独立集.   \(n,m\le10^5 ...

  8. Solution -「洛谷 P4320」道路相遇

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的连通无向图,并给出 \(q\) 个点对 \((u,v)\),询问 \(u\) 到 ...

  9. Solution -「洛谷 P5827」边双连通图计数

    \(\mathcal{Description}\)   link.   求包含 \(n\) 个点的边双连通图的个数.   \(n\le10^5\). \(\mathcal{Solution}\)    ...

随机推荐

  1. mac 操作系统使用iterm(2)自动登录远程服务器

    找一个目录创建一个普通的文件,例如 vi myprofile ,编辑以下内容 #!/usr/bin/expect set PORT 22 set HOST www.****.com(或者ip地址) s ...

  2. 安装Cacti-plugin

    安装pluginunzip cacti-plugin-0.8.7e-PA-v2.6.zip -d cacti-plugin-archcp -R cacti-plugin-arch/* /data/ww ...

  3. 安霸pipeline简述之YUV域的处理

    YUV域处理模块的详细介绍: YUV域的处理主要是rgb_to_yuv_matrix,chroma_scale,ASF(空域降噪),MCTF(时域降噪),SharpenB(锐化模块). RGB2YUV ...

  4. ADO.NET数据访问基础与综合应用2020年10月31日20:17:09学习笔记

    四.创建数据表 1.数据表的名称. 2.表中的字段名.数据类型.是否可以为空.字段的约束.必备的字段(通常会有一个ID,表示实体的唯一性:可以直接手写,也可以使用种子标识自动生成,给定起始值,给定增长 ...

  5. 基于rabbitmq延迟插件实现分布式延迟任务

    承接上文基于redis,redisson的延迟队列实践,今天介绍下基于rabbitmq延迟插件rabbitmq_delayed_message_exchange实现延迟任务. 一.延迟任务的使用场景 ...

  6. 【Java】方法

    文章目录 何谓方法 方法的定义 方法调用 方法重载 命令行传参 可变参数 递归 何谓方法 System.out.println(),是什么 Java方法是语句的集合,它们在一起执行一个功能 方法是解决 ...

  7. DbHelper 标准类

    import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.jsp.jstl.sql.*; public ...

  8. spring拦截机制中Filter(过滤器)、interceptor(拦截器)和Aspect(切面)的使用及区别

    Spring中的拦截机制,如果出现异常的话,异常的顺序是从里面到外面一步一步的进行处理,如果到了最外层都没有进行处理的话,就会由tomcat容器抛出异常. 1.过滤器:Filter :可以获得Http ...

  9. pytest文档4-fixture之conftest.py

    用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了.fixture之conftest.py就是自定义测试用例的预置条件 1.firture相对于 ...

  10. C# 同步 异步 回调 状态机 async await Demo

    源码 https://gitee.com/s0611163/AsyncAwaitDemo 为什么会研究这个? 我们项目的客户端和服务端通信用的是WCF,我就想,能不能用异步的方式调用WCF服务呢?或者 ...