当前位置:网站首页>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
边栏推荐
- 重载、重写、隐藏的对比
- Creation of raid0 and RAID5 and Simulation of how RAID5 works
- Redis 异常 read error on connection 解决方案
- STM32 and FreeRTOS stack parsing
- MySQL of database -- basic common query commands
- [geek challenge 2019] havefun1
- Using JS to realize a thousandth bit
- Comparison of overloading, rewriting and hiding
- Introduction to sap pi / PO login and basic functions
- JS scope, scope chain, global variables and local variables
猜你喜欢

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

Example of data object mask used by SAP translate

Applet error: should have URL attribute when using navigateto, redirectto or switchtab

Go language learning notes - slice, map | go language from scratch

Vivo, hardware safe love and thunder

npm ERR! network

《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路

Random neurons and random depth of dropout Technology

MySQL of database -- Fundamentals

Easy to understand subset DP
随机推荐
ABAP implementation publishes restful services for external invocation example
[educational codeforces round 80] problem solving Report
kettle庖丁解牛第14篇之JSON输入
1 + X cloud computing intermediate -- script construction, read-write separation
MySQL of database -- basic common query commands
Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
(Extended) bsgs and higher order congruence equation
Kettle实验 转换案例
Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
KVM installation and deployment
Using JS to realize a thousandth bit
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
Introduction to sap pi / PO login and basic functions
Go language learning notes - array | go language from scratch
Redis expired key cleaning and deletion policy summary
Comparison of overloading, rewriting and hiding
AQS & reentrantlock implementation principle
nn. Explanation of module class
MySQL of database -- Fundamentals