\[\sum_{i=1}^{n}\sum_{j=1}^{n} ij\gcd(i,j)
\]

\[=\sum_{d=1}^{n} d \sum_{i=1}^{n}\sum_{j=1}^{n} ij[\gcd(i,j)==d]
\]

\[=\sum_{d=1}^{n} d^3 \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor\frac{n}{d}\rfloor} ij[\gcd(i,j)==1]
\]

\[=\sum_{d=1}^{n} d^3 \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor\frac{n}{d}\rfloor} ij[\gcd(i,j)==1]
\]

因为 \(\sum_{t|n} \mu(t) = [n == 1]\)

所以

\[=\sum_{d=1}^{n} d^3 \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor\frac{n}{d}\rfloor} ij \sum_{t|\gcd(i,j)} \mu(t)
\]

\[=\sum_{d=1}^{n} d^3 \sum_{t=1}^{\lfloor\frac{n}{d}\rfloor} \mu(t) t^2 \sum_{i=1}^{\lfloor\frac{n}{td}\rfloor} \sum_{j=1}^{\lfloor\frac{n}{td}\rfloor} ij
\]

设 \(sum_x = \sum_{i=1}^{x}\)

则原式等同于

\[=\sum_{d=1}^{n} d^3 \sum_{t=1}^{\lfloor\frac{n}{d}\rfloor} \mu(t) t^2 (sum_{\lfloor \frac{n}{td} \rfloor})^2
\]

设 \(T=td\)

\[=\sum_{T=1}^{n} T^2 (sum_{\lfloor \frac{n}{T} \rfloor})^2 \sum_{d|t} \mu (\frac{T}{d}) d
\]

因为

\[\sum_{d|n} \frac{\mu(d)}{d} = \frac{\varphi(n)}{n}
\]

根据狄利克雷卷积

\[id(x) = x , id * \mu = \varphi
\]

那么这个柿子变成了

\[\sum_{T=1}^{n}T^2 \varphi(T) (sum_{\lfloor \frac{n}{T} \rfloor})^2
\]

然后我们本身知道 形如 \(\lfloor \frac{n}{T} \rfloor\) 之类的玩意可以数论分块

然后 \(f_x = x^2 \varphi(x)\) 的前缀和

设 \(s_x\) 是 \(f_x\) 的前缀和,然后考虑 \(g\) 函数

然后我们知道

\[\sum_{d|n} \varphi(d) = n
\]

考虑 \(g\) 函数为 \(g_i = i^2\)

\[(g*f)(i) = \sum_{d|i} d^2 \varphi (d) \frac{i^2}{d^2} = \sum_{d|i}\varphi (d) i^2 = i^3
\]

\[s_n = \sum_{i=1}^{n} i^3 - \sum_{i=2}^{n} i^2 s_{\frac{n}{i}}
\]

因为

\[\sum_{i=1}^{n}i^3 = (\sum_{i=1}^{n}i)^2
\]

\[\sum_{i=1}^{n}i^2 = \frac{n(n+1)(2n+1)}{6}
\]

Q.E.D.

杜教筛一手,这题没了。

// powered by c++11
// by Isaunoya
#include<bits/stdc++.h>
#define rep(i , x , y) for(register int i = (x) ; i <= (y) ; ++ i)
#define Rep(i , x , y) for(register int i = (x) ; i >= (y) ; -- i)
using namespace std ;
using db = double ;
using ll = long long ;
using uint = unsigned int ;
#define int long long
using pii = pair < int , int > ;
#define ve vector
#define Tp template
#define all(v) v.begin() , v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp < class T > void cmax(T & x , const T & y) { if(x < y) x = y ; }
Tp < class T > void cmin(T & x , const T & y) { if(x > y) x = y ; }
// sort , unique , reverse
Tp < class T > void sort(ve < T > & v) { sort(all(v)) ; }
Tp < class T > void unique(ve < T > & v) { sort(all(v)) ; v.erase(unique(all(v)) , v.end()) ; }
Tp < class T > void reverse(ve < T > & v) { reverse(all(v)) ; }
const int SZ = 0x191981 ;
struct FILEIN {
~ FILEIN () {} char qwq[SZ] , * S = qwq , * T = qwq , ch ;
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq , 1 , SZ , stdin) , S == T) ? EOF : * S ++ ; }
FILEIN & operator >> (char & c) { while(isspace(c = GETC())) ; return * this ; }
FILEIN & operator >> (string & s) {
while(isspace(ch = GETC())) ; s = "" + ch ;
while(! isspace(ch = GETC())) s += ch ; return * this ;
}
Tp < class T > void read(T & x) {
bool sign = 1 ; while((ch = GETC()) < 0x30) if(ch == 0x2d) sign = 0 ;
x = (ch ^ 0x30) ; while((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30) ;
x = sign ? x : -x ;
}
FILEIN & operator >> (int & x) { return read(x) , * this ; }
FILEIN & operator >> (signed & x) { return read(x) , * this ; }
FILEIN & operator >> (unsigned & x) { return read(x) , * this ; }
} in ;
struct FILEOUT { const static int LIMIT = 0x114514 ;
char quq[SZ] , ST[0x114] ; signed sz , O ;
~ FILEOUT () { sz = O = 0 ; }
void flush() { fwrite(quq , 1 , O , stdout) ; fflush(stdout) ; O = 0 ; }
FILEOUT & operator << (char c) { return quq[O ++] = c , * this ; }
FILEOUT & operator << (string str) {
if(O > LIMIT) flush() ; for(char c : str) quq[O ++] = c ; return * this ;
}
Tp < class T > void write(T x) {
if(O > LIMIT) flush() ; if(x < 0) { quq[O ++] = 0x2d ; x = -x ; }
do { ST[++ sz] = x % 0xa ^ 0x30 ; x /= 0xa ; } while(x) ;
while(sz) quq[O ++] = ST[sz --] ; return ;
}
FILEOUT & operator << (int x) { return write(x) , * this ; }
FILEOUT & operator << (signed x) { return write(x) , * this ; }
FILEOUT & operator << (unsigned x) { return write(x) , * this ; }
} out ; int n , p ;
const int maxn = 5e6 + 10 ;
int phi[maxn + 5] ;
int iv2 , iv6 ;
int qpow(int x , int y) {
int res = 1 ;
for( ; y ; y >>= 1 , x = x * x % p)
if(y & 1)
res = res * x % p ;
return res ;
}
int sum(int x) {
x = x - (x / p) * p ;
return (x + 1) * x % p * iv2 % p ;
}
int sum2(int x) {
x = x - (x / p) * p ;
return (x + 1) * x % p * (x + x + 1) % p * iv6 % p ;
} unordered_map < int , int > _phi ;
int s(int x) {
if(x <= maxn) return phi[x] ;
if(_phi[x]) return _phi[x] ;
int ans = sum(x) ;
ans = ans * ans % p ;
for(int l = 2 , r ; l <= x ; l = r + 1) {
r = x / (x / l) ;
ans = (ans - (sum2(r) - sum2(l - 1) + p) % p * s(x / l) % p + p) % p ;
}
return _phi[x] = ans ;
}
signed main() {
#ifdef _WIN64
freopen("testdata.in" , "r" , stdin) ;
#else
ios_base :: sync_with_stdio(false) ;
cin.tie(nullptr) , cout.tie(nullptr) ;
#endif
// code begin.
in >> p >> n ;
iv2 = qpow(2 , p - 2) ;
iv6 = qpow(6 , p - 2) ;
phi[1] = 1 ;
for(int i = 2 ; i <= maxn; i ++) {
if(phi[i])
continue ;
for(int j = i ; j <= maxn ; j += i) {
if(! phi[j])
phi[j] = j ;
phi[j] = phi[j] / i * (i - 1) ;
}
}
for(int i = 1 ; i <= maxn ; i ++)
phi[i] = (phi[i - 1] + 1ll * i * i % p * phi[i] % p) % p ;
int ans = 0 ;
for(int l = 1 , r ; l <= n ; l = r + 1) {
r = n / (n / l) ;
int k = sum(n / l) ;
k = k * k % p ;
ans = (ans + (s(r) - s(l - 1) + p) % p * k % p) % p ;
}
out << ans << '\n' ;
return out.flush() , 0 ;
// code end.
}

P3768 简单的数学题 [杜教筛,莫比乌斯反演]的更多相关文章

  1. P3768 简单的数学题 杜教筛+推式子

    \(\color{#0066ff}{ 题目描述 }\) 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数n和一个整数p,你需要求出(\(\sum_{i=1}^n\sum_{j=1}^n ij ...

  2. luogu P3768 简单的数学题 杜教筛 + 欧拉反演 + 逆元

    求 $\sum_{i=1}^{n}\sum_{j=1}^{n}ijgcd(i,j)$   考虑欧拉反演: $\sum_{d|n}\varphi(d)=n$   $\Rightarrow \sum_{i ...

  3. BZOJ_4176_Lucas的数论_杜教筛+莫比乌斯反演

    BZOJ_4176_Lucas的数论_杜教筛+莫比乌斯反演 Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么喜欢了. 在整理以前的试题时,发现了这样一道题目“求 ...

  4. [luogu3768] 简单的数学题 [杜教筛]

    题面: 传送门 实际上就是求: 思路: 看到gcd就先反演一下,过程大概是这样: 明显的一步反演 这里设,S(x)等于1到x的和 然后把枚举d再枚举T变成先枚举T再枚举其约数d,变形: 后面其中两项展 ...

  5. 洛谷 - P3768 - 简单的数学题 - 欧拉函数 - 莫比乌斯反演

    https://www.luogu.org/problemnew/show/P3768 \(F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}ijgcd(i ...

  6. [51Nod 1237] 最大公约数之和 (杜教筛+莫比乌斯反演)

    题目描述 求∑i=1n∑j=1n(i,j) mod (1e9+7)n<=1010\sum_{i=1}^n\sum_{j=1}^n(i,j)~mod~(1e9+7)\\n<=10^{10}i ...

  7. 【XSY2731】Div 数论 杜教筛 莫比乌斯反演

    题目大意 定义复数\(a+bi\)为整数\(k\)的约数,当且仅当\(a\)和\(b\)为整数且存在整数\(c\)和\(d\)满足\((a+bi)(c+di)=k\). 定义复数\(a+bi\)的实部 ...

  8. [CQOI2015][bzoj3930] 选数 [杜教筛+莫比乌斯反演]

    题面: 传送门 思路: 首先我们把区间缩小到$\left[\lfloor\frac{L-1}{K}\rfloor,\lfloor\frac{R}{K}\rfloor\right]$ 这道题的最特殊的点 ...

  9. [bzoj 4176] Lucas的数论 (杜教筛 + 莫比乌斯反演)

    题面 设d(x)d(x)d(x)为xxx的约数个数,给定NNN,求 ∑i=1N∑j=1Nd(ij)\sum^{N}_{i=1}\sum^{N}_{j=1} d(ij)i=1∑N​j=1∑N​d(ij) ...

随机推荐

  1. 【大白话系统】MySQL 学习总结 之 缓冲池(Buffer Pool) 的设计原理和管理机制

    一.缓冲池(Buffer Pool)的地位 在<MySQL 学习总结 之 InnoDB 存储引擎的架构设计>中,我们就讲到,缓冲池是 InnoDB 存储引擎中最重要的组件.因为为了提高 M ...

  2. 动手学习Pytorch(4)--过拟合欠拟合及其解决方案

    过拟合.欠拟合及其解决方案 过拟合.欠拟合的概念 权重衰减 丢弃法   模型选择.过拟合和欠拟合 训练误差和泛化误差 在解释上述现象之前,我们需要区分训练误差(training error)和泛化误差 ...

  3. 06-Spring03-事务管理

    今日知识 1. Spring事务管理 2. 转账案例 Spring事务管理 1. 事务特性(ACID) 1. 原子性:整体 [原子性是指事务包含的所有操作要么全部成功,要么全部失败] 2. 一致性:数 ...

  4. GDB gdb 调试

    除了用grmon看汇编调试外,还可以用gdb. 编译的时候加-g gdb app即可进入gdb调试 设置断点:b main.c:10 然后运行程序:run 断点处可以查看变量:display a 其它 ...

  5. k8s系列---ingress资源和ingress-controller

    https://www.cnblogs.com/zhangeamon/p/7007076.html http://blog.itpub.net/28916011/viewspace-2214747/ ...

  6. k8s系列---部署集群

    docer启动出错 [root@centos-minion yum.repos.d]# systemctl start docker Job for docker.service failed bec ...

  7. day14 jQuery

    day13-14 jQueryjQuery是对js和dom的封装,相当于一个类库使用jQuery的目的: 1:获取标签 2:修改jQuery提供的方法:http://www.php100.com/ma ...

  8. 杭电-------2032杨辉三角(C语言写)

    #include<stdio.h> ][] = { }; void init() { int i, j; ; i < ; i++) { a[i][] = ; a[i][i] = ; ...

  9. 不会用数据可视化大屏?一招教你轻松使用数据可视化BI软件创建农业公司运营数据分析大屏

    灯果数据可视化BI软件是新一代人工智能数据可视化大屏软件,内置丰富的大屏模板,可视化编辑操作,无需任何经验就可以创建属于你自己的大屏.大家可以在他们的官网下载软件.   本文以农业公司运营数据分析大屏 ...

  10. js this是什么?[多次书写]

    前言 以前的时候,我写了一个关于js this的博客,写的非常复杂,分析了各种情况. 现在我想简化. 如果你有后台基础,专门去理解过this,那么请忘记. 这东西是有口诀的: 在方法中,this 表示 ...