[51Nod1238]最小公倍数之和 V3[杜教筛]
题意
给定 \(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}\),所以
\]
我们记 \(f(i)=i^2\varphi (i), S(n)=\sum_{i=1}^nf(i)\),那么原式变形为:
\]
考虑用杜教筛求 \(S\) ,根据:
\]
为了消去 \(i^2\) 的影响,我们考虑构造 \(g(i)=i^2\),得到:
\]
杜教筛上式,外层数论分块即可。
复杂度就不算了哈
代码
#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[杜教筛]的更多相关文章
- 51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...
- 【51nod】1238 最小公倍数之和 V3 杜教筛
[题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...
- 51 Nod 1238 最小公倍数之和 V3 杜教筛
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1238 题意:求$\sum_{i=1}^{n}\sum_{j=1}^{n}l ...
- 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 ...
- [51Nod 1238] 最小公倍数之和 (恶心杜教筛)
题目描述 求∑i=1N∑j=1Nlcm(i,j)\sum_{i=1}^N\sum_{j=1}^Nlcm(i,j)i=1∑Nj=1∑Nlcm(i,j) 2<=N<=10102<=N ...
- 51nod 237 最大公约数之和 V3 杜教筛
Code: #include <bits/stdc++.h> #include <tr1/unordered_map> #define setIO(s) freopen(s&q ...
- 51nod1238 最小公倍数之和 V3 莫比乌斯函数 杜教筛
题意:求\(\sum_{i = 1}^{n}\sum_{j = 1}^{n}lcm(i, j)\). 题解:虽然网上很多题解说用mu卡不过去,,,不过试了一下貌似时间还挺充足的,..也许有时间用phi ...
- 51nod 1244 莫比乌斯函数之和 【杜教筛】
51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...
- 51nod 1244 莫比乌斯函数之和(杜教筛)
[题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...
随机推荐
- Hibernate中对象的三种状态及相互转化
1. 瞬时状态 (Transient) 当我们通过Java的new关键字来生成一个实体对象时,这时这个实体对象就处于自由状态,如下: Customer customer=new Customer(“ ...
- SQLSERVER NULL和空字符串的区别 使用NULL是否节省空间
SQLSERVER NULL和空字符串的区别 使用NULL是否节省空间 这里只讨论字符串类型,int.datetime.text这些数据类型就不讨论了,因为是否节省空间是根据数据类型来定的 在写这篇文 ...
- 使用 Azure PowerShell 管理 Azure 虚拟网络和 Windows 虚拟机
Azure 虚拟机使用 Azure 网络进行内部和外部网络通信. 本教程介绍了如何在虚拟网络中创建多个虚拟机 (VM),以及如何在虚拟机之间配置网络连接. 你将学习如何执行以下操作: 创建虚拟网络 创 ...
- Oracle EBS OPM 取消生产批
--取消生产批 --created by jenrry SET serveroutput on; DECLARE p_batch_header_rec gme_batch_header%ROWTYPE ...
- centOS7中Mariadb数据库安装与基本管理
一.Mariadb数据库安装 1. 直接yum源安装 yum -y install mariadb mariadb-serversystemctl start mariadb /启动Mariadb服务 ...
- 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)
C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...
- Ctrl+Alt+F1~F6
Ctrl+ALT+F1~F6 可以进入不同的字符终端和图形界面.体现了 linux 或者 unix 的多用户的特点. 6个不同的终端,相当于六个不同的用户. 保持更新,转载请著名出处.
- ulimit linux文件配置
文件描述符在形式上是一个非负整数.实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表.当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符.在程序设计中,一 ...
- 1001.A+B Format(10)
1001.A+B Format(20) github链接:[example link](https://github.com/wgc12/object-oriented 1.对题目的理解: 首先这道题 ...
- SVN 图标不显示的解决办法
SVN 的图标没办法显示了.经搜索,发现需要修改注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Sh ...