当前位置:网站首页>P1390 sum of common divisor (Mobius inversion)
P1390 sum of common divisor (Mobius inversion)
2022-04-23 09:42:00 【Zimba_】
The question :
Given n n n, seek ∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) \sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j) ∑i=1n∑j=i+1ngcd(i,j). ( n < = 2 e 6 ) (n<=2e6) (n<=2e6)
deduction :
First step , Follow the routine first g c d gcd gcd Bring it to the front ,
∑ g = 1 n g ∑ i = 1 n ∑ j = i + 1 n [ g = g c d ( i , j ) ] \sum_{g=1}^{n}g\sum_{i=1}^{n}\sum_{j=i+1}^{n}[g=gcd(i,j)] ∑g=1ng∑i=1n∑j=i+1n[g=gcd(i,j)]
The second step , We make f ( g ) = ∑ i = 1 n ∑ j = i + 1 n [ g = g c d ( i , j ) ] f(g)=\sum_{i=1}^{n}\sum_{j=i+1}^{n}[g=gcd(i,j)] f(g)=∑i=1n∑j=i+1n[g=gcd(i,j)], What we ask for at this time is ∑ g = 1 n g f ( g ) \sum_{g=1}^{n}gf(g) ∑g=1ngf(g), Put it first. .
Then at this time, we choose one from the formula of Mobius inversion , Once you have experience, you will know which one to choose .
We make ,
F ( g ) = ∑ g ∣ d f ( d ) F(g)=\sum_{g|d}f(d) F(g)=∑g∣df(d)
= ∑ g ∣ d ∑ i = 1 n ∑ j = i + 1 n [ d = g c d ( i , j ) ] \;\;\;\;\;\;\;\;=\sum_{g|d}\sum_{i=1}^{n}\sum_{j=i+1}^{n}[d=gcd(i,j)] =∑g∣d∑i=1n∑j=i+1n[d=gcd(i,j)]
= ∑ i = 1 n ∑ j = i + 1 n [ g ∣ g c d ( i , j ) ] \;\;\;\;\;\;\;\;=\sum_{i=1}^{n}\sum_{j=i+1}^{n}[g|gcd(i,j)] =∑i=1n∑j=i+1n[g∣gcd(i,j)]
= ∑ i = 1 n ∑ j = i + 1 n [ g ∣ i ∧ g ∣ j ] \;\;\;\;\;\;\;\;=\sum_{i=1}^{n}\sum_{j=i+1}^{n}[g|i\wedge g|j] =∑i=1n∑j=i+1n[g∣i∧g∣j]
= 1 2 ( ⌊ n g ⌋ ⌊ n g ⌋ − ⌊ n g ⌋ ) \;\;\;\;\;\;\;\;=\frac{1}{2} (\lfloor \frac{n}{g} \rfloor \lfloor \frac{n}{g} \rfloor -\lfloor \frac{n}{g} \rfloor) =21(⌊gn⌋⌊gn⌋−⌊gn⌋)
According to the inversion formula , Yes f ( g ) = ∑ g ∣ d μ ( d g ) F ( d ) f(g)=\sum_{g|d}\mu(\frac{d}{g})F(d) f(g)=∑g∣dμ(gd)F(d)
So let's go back to the original formula ,
∑ g = 1 n g f ( g ) = ∑ g n ∑ g ∣ d μ ( d g ) F ( d ) \sum_{g=1}^{n}gf(g)=\sum_{g}^{n}\sum_{g|d}\mu(\frac{d}{g})F(d) ∑g=1ngf(g)=∑gn∑g∣dμ(gd)F(d)
Pushed ahead , F ( d ) F(d) F(d) Sure o ( 1 ) o(1) o(1) count , Mobius function can preprocess , So the final formula can o ( n l o g n ) o(nlogn) o(nlogn) Come on .
Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
typedef pair<int,int> P;
const ll MAXN=2000000;
ll prime[MAXN+10],notPrime[MAXN+10],mu[MAXN+10],tot;
void initMu(ll n)
{
notPrime[1]=mu[1]=1;
for(ll i=2;i<=n;i++)
{
if(!notPrime[i])prime[tot++]=i,mu[i]=-1;
for(ll j=0;j<tot&&i*prime[j]<=n;j++)
{
notPrime[i*prime[j]]=1;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else {
mu[i*prime[j]]=0;break;}
}
}
}
ll n;
ll getF(ll g)
{
ll t=n/g;
return (t*t-t)/2;
}
int main()
{
initMu(MAXN);
scanf("%lld",&n);
ll ans=0;
for(ll g=1;g<=n;g++)
{
for(ll d=g;d<=n;d+=g)
{
ans=ans+g*mu[d/g]*getF(d);
}
}
printf("%lld\n",ans);
return 0;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621425292.html
边栏推荐
- 108. Convert an ordered array into a binary search tree
- Kettle实验
- [geek challenge 2019] havefun1
- AQS & reentrantlock implementation principle
- Set the maximum width of the body, but why does the background color of the body cover the whole page?
- 112. Path sum
- GUI, CLI and UNIX Philosophy
- Summary of common concepts and problems of linear algebra in postgraduate entrance examination
- (Extended) bsgs and higher order congruence equation
- Expansion of number theory Euclid
猜你喜欢
![Sql1 [geek challenge 2019]](/img/ad/afca09bc1da003393319af700e90e3.png)
Sql1 [geek challenge 2019]

Redis 内存占满导致的 Setnx 命令执行失败

Installation of data cleaning ETL tool kettle

108. 将有序数组转换为二叉搜索树

JSON input of Chapter 14 of kettle paoding jieniu

Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
![3、 6 [Verilog HDL] gate level modeling of basic knowledge](/img/36/46f2413ecb12f81c003848c93f6bc9.jpg)
3、 6 [Verilog HDL] gate level modeling of basic knowledge

亚马逊云科技入门资源中心,从0到1轻松上云

SAP debug debug for in, reduce and other complex statements

Simple understanding of arguments in JS
随机推荐
Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
Machine learning (VI) -- Bayesian classifier
Kettle实验 转换案例
Redis exception read error on connection solution
Little girl walking
Buuctf [actf2020 freshman competition] include1
What is monitoring intelligent playback and how to use intelligent playback to query video recording
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
Go language learning notes - structure | go language from scratch
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
Leetcode题库78. 子集(递归 c实现)
KVM installation and deployment
Secrets in buffctf file 1
Canary publishing using ingress
Exclusive thoughts and cases of JS
C语言:表达式求值(整型提升、算术转换 ...)
Alibaba cloud architects interpret the four mainstream game architectures
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
Kettle experiment (III)
Image processing in opencv -- Introduction to contour + contour features