题意

给定 \(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. Sql server bulk insert

    Bulk Insert Sql server 的bulk insert语句可以高效的导入大数据量的平面文件(txt,csv文件)到数据库的一张表中,其用法如下: bulk insert test fr ...

  2. Win7 user profile cant logon

    1.local user:testlb1 1234@cat can login safe model 1.重新启动计算机开机时连续点击F8,选择进入安全模式.2.开始-在搜索栏中输入services. ...

  3. kubeadm快速部署Kubernetes单节点

    1. 安装要求 在开始之前,部署Kubernetes集群机器需要满足以下几个条件: 一台或多台机器,操作系统 CentOS7.x-86_x64 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬 ...

  4. windows Server 2012/2016 路由和远程访问,PPPOE,ADSL,连接接口时出现一个错误,连接被远程计算机终止

    经过查询资料,是由mprddm.dll的bug引起的. 修改位置: 将je修改为jmp. 查找修改位置,可参考 前面的RasGetPortUserData的调用,或者 后面的 字符串 64位dll可使 ...

  5. SDN期末作业验收

    作业链接:https://edu.cnblogs.com/campus/fzu/SoftwareDefinedNetworking2017/homework/1585 负载均衡程序 1.github链 ...

  6. postgresql----几何类型和函数

    postgresql支持的几何类型如下表: 名字 存储空间 描述 表现形式 point 16字节 平面上的点 (x,y) line 32字节 直线 {A,B,C} lseg 32字节 线段 ((x1, ...

  7. app的描述-软件的描述

    app的描述=需求文档+接口文档+程序架构+工程结构.   程序架构:类结构图: 需求文档:业务逻辑-->时序图.

  8. Volley源码分析(三)NetWorkDispatcher分析

    NetWorkDispatcher分析 NetWorkDispatcher和CacheDispatcher一样,继承于Thread,在run方法中实现一个无限循环,代码如下 @Override pub ...

  9. BZOJ4923:[Lydsy1706月赛]K小值查询(Splay)

    Description 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. In ...

  10. 6、JVM--类文件结构(上)

    6.1.概述 写的程序需要经编译器翻译成由0和1构成的二进制格式才能由计算机执行 6.2.无关性基石 Java在刚刚诞生之时曾经提出过一个非常著名的宣传口号:“一次编写,到处运行(Write Once ...