当前位置:网站首页>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
边栏推荐
- 《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
- Kettle experiment
- 【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
- Two ways for flutter providers to share data
- Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice
- JS node operation, why learn node operation
- 1 + X cloud computing intermediate -- script construction, read-write separation
- 成功的DevOps Leader 应该清楚的3个挑战
- JS scope, scope chain, global variables and local variables
- Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
猜你喜欢
Go language learning notes - exception handling | go language from scratch
The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to
Kettle实验 (三)
STM32 and FreeRTOS stack parsing
ABAP publishes OData service samples from CDs view
Go language learning notes - structure | go language from scratch
Kettle实验 转换案例
数据清洗 ETL 工具Kettle的安装
Kettle experiment
[geek challenge 2019] havefun1
随机推荐
ES-aggregation聚合分析
RSA encryption and decryption signature verification
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——4视觉系统中的多故障
Flutter 的加载动画这么玩更有趣
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
Your guide to lowering your cholesterol with TLC (continuously updated)
MacOS下使用CLion编译调试MySQL8.x
[geek challenge 2019] havefun1
DMP engine work summary (2021, lightsaber)
个人主页软件Fenrus
GUI, CLI and UNIX Philosophy
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
Exclusive thoughts and cases of JS
《信息系统项目管理师总结》第八章 项目干系人管理
Three ways to create objects in JS
How to render web pages
Principle of synchronized implementation
MySQL - Chapter 1 (data types in MySQL)
Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail