题意

给定 \(n\) ,求 \(\sum_{i=1}^n \sum_{j=1}^n lcm(i,j)\)。

\(n\leq 10^{10}\)

分析

  • 推式子
    \[\begin{aligned} ans &= 2\sum_{i=1}^n\sum_{j=1}^ilcm(i,j)-\sum_{i=1}^nlcm(i,i) \\ &=2\sum_{i=1}^n \sum_{j=1}^i \frac{ij}{(i,j)}-\frac{n(n+1)}{2} \\ &=2\sum_{d=1}^n\frac{1}{d}\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{i}[i\perp j]ijd^2-\frac{n(n+1)}{2} \\&=2\sum_{d=1}^nd\sum_{i=1}^{\frac{n}{d}}i\sum_{j=1}^{i}[i\perp j]j-\frac{n(n+1)}{2}\end{aligned}
    \]

又因为 \(\sum_{i=1}^n[i \perp n]i \ =\frac{n\varphi (n)+[n=1]}{2}\),所以

\[\begin{aligned} ans &=2\sum_{d=1}^nd\sum_{i=1}^{\frac{n}{d}}\frac{i^2\varphi (i)+[i=1]}{2}-\frac{n(n+1)}{2}\\ &=\sum_{d=1}^nd\sum_{i=1}^{\frac{n}{d}}i^2\varphi (i) \end{aligned}
\]

我们记 \(f(i)=i^2\varphi (i), S(n)=\sum_{i=1}^nf(i)\),那么原式变形为:

\[\sum_{d=1}^nS(\frac{n}{d})d
\]

考虑用杜教筛求 \(S\) ,根据:

\[g(1)S(n)=\sum_{i=1}^n\sum_{j|i}f(j)g(\frac{i}{j})+\sum_{i=2}^ng(i)S(\frac{n}{i})
\]

为了消去 \(i^2\) 的影响,我们考虑构造 \(g(i)=i^2\),得到:

\[\begin{aligned}S(n)&=\sum_{i=1}^n\sum_{j|i}j^2\varphi(j)\frac{i^2}{j^2}+\sum_{i=2}^ni^2S(\frac{n}{i}) \\ &=\sum_{i=1}^ni^3+\sum_{i=2}^ni^2S(\frac{n}{i})\end{aligned}
\]

杜教筛上式,外层数论分块即可。

复杂度就不算了哈

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define go(u) for(int i = head[u], v = e[i].to; i; i=e[i].lst, v=e[i].to)
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define pb push_back
inline int gi() {
int x = 0,f = 1;
char ch = getchar();
while(!isdigit(ch)) {
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - 48;
ch = getchar();
}
return x * f;
}
template <typename T> inline void Max(T &a, T b){if(a < b) a = b;}
template <typename T> inline void Min(T &a, T b){if(a > b) a = b;}
const LL mod = 1e9 + 7, N = 2e6 + 7;
LL n, pc, inv2 = 500000004, inv6;
LL f[N], pri[N];
bool vis[N];
map<LL, LL> F;
LL Pow(LL a, LL b) {
LL res = 1ll;
for(; b; b >>= 1, a = a * a %mod) if(b & 1) res = res * a % mod;
return res;
}
void pre(int n) {
f[1] = 1;int to;
for(int i = 2; i <= n; ++i) {
if(!vis[i]) { pri[++pc] = i, f[i] = 1ll * i * i % mod * (i - 1) % mod; }
for(int j = 1; j <= pc && (to = i * pri[j]) <= n; ++j) {
vis[to] = 1;
if(i % pri[j] == 0) {
f[to] = f[i] * pri[j] % mod * pri[j] % mod * pri[j] %mod;
break;
}
f[to] = f[i] * f[pri[j]] % mod;
}
}
for(int i = 2; i <= n; ++ i) f[i] = (f[i - 1] + f[i]) % mod;
}
LL s3(LL n) {
n %= mod;
LL x = (n + 1) * n % mod * inv2 %mod;
return x * x % mod;
}
LL s2(LL n) {
n %= mod;
return n * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod;
}
LL s1(LL n) {
n %= mod;
return n * (n + 1) % mod * inv2 % mod;
}
void add(LL &a, LL b){ a += b; if(a >= mod) a -= mod;}
LL gg(LL n) {
if(n <= 2e6) return f[n];
if(F.count(n)) return F[n];
LL res = s3(n);
for(LL i = 2, lst = 2; i <= n; i = lst + 1) {
lst = n / (n / i);
add(res, mod - (s2(lst) - s2(i - 1) + mod) * gg(n / i) %mod);
}
return F[n] = res;
}
LL solve(LL n) {
LL res = 0ll;
for(LL i = 1, lst = 1; i <= n; i = lst + 1) {
lst = n / (n / i);
add(res, (s1(lst) - s1(i - 1) + mod) * gg(n / i) % mod);
}
return res;
}
int main() {
scanf("%lld", &n);
inv6 = Pow(6, mod - 2);
pre(2e6);
printf("%lld\n", (solve(n) % mod + mod) % mod);
return 0;
}

[51Nod1238]最小公倍数之和 V3[杜教筛]的更多相关文章

  1. 51NOD 1238 最小公倍数之和 V3 [杜教筛]

    1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...

  2. 【51nod】1238 最小公倍数之和 V3 杜教筛

    [题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...

  3. 51 Nod 1238 最小公倍数之和 V3 杜教筛

    题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1238 题意:求$\sum_{i=1}^{n}\sum_{j=1}^{n}l ...

  4. 51NOD 1237 最大公约数之和 V3 [杜教筛]

    1237 最大公约数之和 V3 题意:求\(\sum_{i=1}^n\sum_{j=1}^n(i,j)\) 令\(A(n)=\sum_{i=1}^n(n,i) = \sum_{d\mid n}d \c ...

  5. [51Nod 1238] 最小公倍数之和 (恶心杜教筛)

    题目描述 求∑i=1N∑j=1Nlcm(i,j)\sum_{i=1}^N\sum_{j=1}^Nlcm(i,j)i=1∑N​j=1∑N​lcm(i,j) 2<=N<=10102<=N ...

  6. 51nod 237 最大公约数之和 V3 杜教筛

    Code: #include <bits/stdc++.h> #include <tr1/unordered_map> #define setIO(s) freopen(s&q ...

  7. 51nod1238 最小公倍数之和 V3 莫比乌斯函数 杜教筛

    题意:求\(\sum_{i = 1}^{n}\sum_{j = 1}^{n}lcm(i, j)\). 题解:虽然网上很多题解说用mu卡不过去,,,不过试了一下貌似时间还挺充足的,..也许有时间用phi ...

  8. 51nod 1244 莫比乌斯函数之和 【杜教筛】

    51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...

  9. 51nod 1244 莫比乌斯函数之和(杜教筛)

    [题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...

随机推荐

  1. Inception开源公告

    关于Inception MySQL语句的审核,在业界都已经基本被认同了,实际上也是对MySQL语句写法的统一化,标准化,而之前的人工审核,针对标准这个问题其实是很吃力的,标准越多,DBA越累,开发也越 ...

  2. jboss eap6.1(5)(ejb升级)

    以前的项目是基于ejb2.x做的,ejb的配置文件为ejb-jar.xml和jboss.xml,现在把这个项目移到新版本服务器中的时候,报解析ejb-jar错误. 查阅许多资料才找到解决办法,原来jb ...

  3. CSS| table property

  4. percona pt toolkit 总结

    ##=====================================================##pt-osc之工作流程:1.检查更改表是否有主键或唯一索引,是否有触发器2.检查修改表 ...

  5. linux开机步骤

    linux开机启动步骤: 1.bios自检 2.MBR引导 3.引导系统,进入grub菜单 4.加载内核kernel 5.运行第一个进程init 6.读取/etc/inittab 读取运行级别 7.读 ...

  6. malloc,calloc,realloc函数用法,原理及不同解析

    https://blog.csdn.net/lixungogogo/article/details/50887028 一.malloc malloc在MSDN中原型为: void *malloc( s ...

  7. JList动态添加元素

    JList动态添加元素   http://www.cnblogs.com/tianguook/archive/2012/01/31/2333992.html https://zhuanlan.zhih ...

  8. mysql数据库中导入txt文本数据的方法

     安装好MySQL和Navicat 8 for MySQL 通过Navicat 8 for MySQL创建数据库test. 2 在数据库test上创建测试数据表student(主键ID,姓名,年龄,学 ...

  9. MySQL基础值 存储过程和函数

    一.创建存储过程和函数 什么是创建存储过程和函数? 就是将经常使用的一组SQL语句组合在一起,并将这些SQL语句当做一个整体存储在MYSQL服务器中. 创建存储过程的语句是:CREATE  PROCE ...

  10. Javaweb学习(二):Http通信协议

      当我们开始jsp/servlet编程之旅之前,我们还需要知道一些关于网络通讯方面的一些知识.这样能更加有助于我们的理解,希望大家能看懂我的描述,而不至于在学习的路上一知半解.(手动比❤) 认识Ht ...