当前位置:网站首页>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
边栏推荐
- Acquisition of DOM learning elements JS
- Kettle实验 (三)
- How to use SQL statement union to get another column of another table when the content of a column in a table is empty
- Expansion of number theory Euclid
- 108. Convert an ordered array into a binary search tree
- Go language learning notes - slice, map | go language from scratch
- KVM installation and deployment
- Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
- Es aggregation aggregation analysis
- Go language learning notes - structure | go language from scratch
猜你喜欢
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core
亚马逊云科技入门资源中心,从0到1轻松上云
Principle of synchronized implementation
SAP ECC connecting SAP pi system configuration
#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
ABAP CDs view with association example
SAP pi / PO function operation status monitoring and inspection
Go language learning notes - exception handling | go language from scratch
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
随机推荐
Acquisition of DOM learning elements JS
SAP CR transmission request sequence and dependency check
Easy to understand subset DP
Example of data object mask used by SAP translate
Leetcode0587. 安装栅栏(difficult)
1 + X cloud computing intermediate -- script construction, read-write separation
501. Mode in binary search tree
Applet error: cannot read property'currenttarget'of undefined
GUI, CLI and UNIX Philosophy
Learn FPGA (from Verilog to HLS)
JS and how to judge custom attributes in H5
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
Es aggregation aggregation analysis
Less than 100 secrets about prime numbers
Redis exception read error on connection solution
Go language learning notes - array | go language from scratch
《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
个人主页软件Fenrus
What is monitoring intelligent playback and how to use intelligent playback to query video recording
SAP pi / PO function operation status monitoring and inspection