当前位置:网站首页>GCD of p2257 YY (Mobius inversion)
GCD of p2257 YY (Mobius inversion)
2022-04-23 09:42:00 【Zimba_】
The question :
Given N , M N, M N,M, seek 1 ≤ x ≤ N , 1 ≤ y ≤ M 1 \leq x \leq N,1 \leq y \leq M 1≤x≤N,1≤y≤M And gcd ( x , y ) \gcd(x, y) gcd(x,y) Prime number ( x , y ) (x, y) (x,y) How many pairs? .
T T T Group example , T = 1 0 4 , N , M ≤ 1 0 7 T = 10^{4} ,N,M\leq10^{7} T=104,N,M≤107
deduction :
Write the formula according to the meaning of the question ,
∑ i = 1 N ∑ j = 1 M [ g c d ( i , j ) by quality Count ] \sum_{i=1}^{N}\sum_{j=1}^{M}[gcd(i,j) Prime number ] ∑i=1N∑j=1M[gcd(i,j) by quality Count ]
Put forward g c d gcd gcd, become
∑ g = 1 m i n ( N , M ) ∑ i = 1 N ∑ j = 1 M [ g c d ( i , j ) = g ] [ g by quality Count ] \sum_{g=1}^{min(N,M)}\sum_{i=1}^{N}\sum_{j=1}^{M}[gcd(i,j)=g][g Prime number ] ∑g=1min(N,M)∑i=1N∑j=1M[gcd(i,j)=g][g by quality Count ]
Let's make N < M N<M N<M
It becomes ∑ g = 1 N [ g by quality Count ] ∑ i = 1 N ∑ j = 1 M [ g c d ( i , j ) = g ] \sum_{g=1}^{N}[g Prime number ]\sum_{i=1}^{N}\sum_{j=1}^{M}[gcd(i,j)=g] ∑g=1N[g by quality Count ]∑i=1N∑j=1M[gcd(i,j)=g] 了 , It's more comfortable to write .
Make f ( g ) = ∑ i = 1 N ∑ j = 1 M [ g c d ( i , j ) = g ] f(g)=\sum_{i=1}^{N}\sum_{j=1}^{M}[gcd(i,j)=g] f(g)=∑i=1N∑j=1M[gcd(i,j)=g]
Make F ( g ) = ∑ g ∣ d f ( d ) = ∑ i = 1 N ∑ j = 1 M [ g ∣ g c d ( i , j ) ] = ⌊ N g ⌋ ⌊ M g ⌋ F(g)=\sum_{g|d}f(d)=\sum_{i=1}^{N}\sum_{j=1}^{M}[g|gcd(i,j)]=\lfloor \frac{N}{g}\rfloor \lfloor\frac{M}{g} \rfloor F(g)=∑g∣df(d)=∑i=1N∑j=1M[g∣gcd(i,j)]=⌊gN⌋⌊gM⌋
According to Mobius inversion formula, there are 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)
Replace the original formula to get ,
∑ g = 1 N [ g by quality Count ] f ( g ) = ∑ g = 1 N [ g by quality Count ] ∑ g ∣ d μ ( d g ) ⌊ N d ⌋ ⌊ M d ⌋ \sum_{g=1}^{N}[g Prime number ]f(g)=\sum_{g=1}^{N}[g Prime number ]\sum_{g|d}\mu(\frac{d}{g})\lfloor \frac{N}{d}\rfloor \lfloor\frac{M}{d} \rfloor ∑g=1N[g by quality Count ]f(g)=∑g=1N[g by quality Count ]∑g∣dμ(gd)⌊dN⌋⌊dM⌋
= ∑ d = 1 N ⌊ N d ⌋ ⌊ M d ⌋ ∑ g ∣ d μ ( d g ) [ g by quality Count ] =\sum_{d=1}^{N}\lfloor \frac{N}{d}\rfloor \lfloor\frac{M}{d} \rfloor\sum_{g|d}\mu(\frac{d}{g})[g Prime number ] =∑d=1N⌊dN⌋⌊dM⌋∑g∣dμ(gd)[g by quality Count ]
= ∑ d = 1 N ⌊ N d ⌋ ⌊ M d ⌋ S ( d ) =\sum_{d=1}^{N}\lfloor \frac{N}{d}\rfloor \lfloor\frac{M}{d} \rfloor S(d) =∑d=1N⌊dN⌋⌊dM⌋S(d)
Among them, we make S ( d ) = ∑ g ∣ d μ ( d g ) [ g by quality Count ] S(d)=\sum_{g|d}\mu(\frac{d}{g})[g Prime number ] S(d)=∑g∣dμ(gd)[g by quality Count ], We just need to preprocess out S ( d ) S(d) S(d), Reprocess out S ( d ) S(d) S(d) And , You can use number theory to find the answer in blocks .
So pretreatment S ( d ) S(d) S(d), We can calculate the contribution for each number decomposition prime factor , Or for each prime number , Add the contribution to all its multiples , Either way o ( n l o g n ) o(nlogn) o(nlogn).
Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
typedef pair<int,int> P;
const int MAXN=10000000;
int prime[MAXN+10],notPrime[MAXN+10],mu[MAXN+10],tot;
void initMu(int n)
{
notPrime[1]=mu[1]=1;
for(int i=2;i<=n;i++)
{
if(!notPrime[i])prime[tot++]=i,mu[i]=-1;
for(int 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;}
}
}
}
int f[MAXN+10];
int main()
{
initMu(MAXN);
for(int i=1;i<=MAXN;i++)if(!notPrime[i])
{
for(int j=i;j<=MAXN;j+=i)
{
f[j]+=mu[j/i];
}
}
for(int i=1;i<=MAXN;i++)f[i]+=f[i-1];
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
if(n>m)swap(n,m);
ll ans=0;
for(int l=1,r;l<=n;l=r+1)
{
r=min(n/(n/l),m/(m/l));
ans=ans+1ll*(n/l)*(m/l)*(f[r]-f[l-1]);
}
printf("%lld\n",ans);
}
return 0;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621425251.html
边栏推荐
- Kettle experiment (III)
- ABAP publishes OData service samples from CDs view
- Kettle实验 转换案例
- AQS & reentrantlock implementation principle
- Expansion of number theory Euclid
- 《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
- Cloud computing competition -- basic part of 2020 competition [task 3]
- NLLLoss+log_ SoftMax=CE_ Loss
- Leetcode0587. Install fence
- Introduction to sap pi / PO login and basic functions
猜你喜欢

Secrets in buffctf file 1

Using JS to realize a thousandth bit

Random neurons and random depth of dropout Technology

SAP ECC connecting SAP pi system configuration

云身份过于宽松,为攻击者打开了大门

Emuelec compilation summary

Kettle实验 (三)

C语言:表达式求值(整型提升、算术转换 ...)

《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路

Kernel PWN learning (4) -- double fetch & 0ctf2018 baby
随机推荐
Set the maximum width of the body, but why does the background color of the body cover the whole page?
DVWA range practice
kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
Principle of synchronized implementation
Three ways to create objects in JS
Buuctf [actf2020 freshman competition] include1
Where is int a = 1 stored
SAP 03-amdp CDs table function using 'with' clause
SAP ECC connecting SAP pi system configuration
Flutter's loading animation is more interesting
Three challenges that a successful Devops leader should be aware of
JSON input of Chapter 14 of kettle paoding jieniu
Redis expired key cleaning and deletion policy summary
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
Give the method of instantiating the object to the new object
Vivo, hardware safe love and thunder
JS prototype chain
Chapter VIII project stakeholder management of information system project manager summary
JS node operation, why learn node operation