当前位置:网站首页>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
边栏推荐
- Go language learning notes - structure | go language from scratch
- Leetcode题库78. 子集(递归 c实现)
- Compile and debug mysql8 with clion under MacOS x
- Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
- 1 + X cloud computing intermediate -- script construction, read-write separation
- Leetcode0587. Install fence
- 代码源每日一题 div1 (701-707)
- 《信息系统项目管理师总结》第八章 项目干系人管理
- Redis expired key cleaning and deletion policy summary
- Kettle实验 转换案例
猜你喜欢
Simple understanding of arguments in JS
Practice of Flink streaming batch integration in Xiaomi
Node installation
Go language learning notes - language interface | go language from scratch
Principle of synchronized implementation
SAP 03-amdp CDs table function using 'with' clause
DVWA range practice
Educational Codeforces Round 81 (Rated for Div. 2)
云身份过于宽松,为攻击者打开了大门
Vivo, hardware safe love and thunder
随机推荐
DMP engine work summary (2021, lightsaber)
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
DVWA range practice record
亚马逊云科技入门资源中心,从0到1轻松上云
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——4视觉系统中的多故障
Sql1 [geek challenge 2019]
JS scope, scope chain, global variables and local variables
Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
Secrets in buffctf file 1
代码源每日一题 div1 (701-707)
理解作用域
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
SAP pi / PO soap2proxy consumption external WS example
Kettle experiment (III)
Flutter's loading animation is more interesting
成功的DevOps Leader 应该清楚的3个挑战
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
SAP pi / PO function operation status monitoring and inspection