当前位置:网站首页>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
边栏推荐
- 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
- JS prototype chain
- PHP notes (I): development environment configuration
- Number theory blocking (integer division blocking)
- MySQL - Chapter 1 (data types in MySQL)
- 112. Path sum
- Leetcode question bank 78 Subset (recursive C implementation)
- 如何实现根据照片获取地理位置及如何防御照片泄漏地理位置
- Creation of raid0 and RAID5 and Simulation of how RAID5 works
- Canary publishing using ingress
猜你喜欢
![3、 6 [Verilog HDL] gate level modeling of basic knowledge](/img/36/46f2413ecb12f81c003848c93f6bc9.jpg)
3、 6 [Verilog HDL] gate level modeling of basic knowledge

Exclusive thoughts and cases of JS

JS DOM event

High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?

个人主页软件Fenrus

Kettle experiment

Leetcode question bank 78 Subset (recursive C implementation)

Educational Codeforces Round 81 (Rated for Div. 2)

Using sqlmap injection to obtain the account and password of the website administrator

501. Mode in binary search tree
随机推荐
Principle of synchronized implementation
Using JS to realize a thousandth bit
ES-aggregation聚合分析
How to render web pages
Codeforces Round #784 (Div. 4)
PHP笔记(一):开发环境配置
ABAP 7.4 SQL Window Expression
JS case to find the maximum value, reverse the array, bubble sort
AI上推荐 之 MMOE(多任务yyds)
Go language learning notes - language interface | go language from scratch
Pre parsing of JS
代码源每日一题 div1 (701-707)
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——4视觉系统中的多故障
JSON input of Chapter 14 of kettle paoding jieniu
Es aggregation aggregation analysis
Creation of raid0 and RAID5 and Simulation of how RAID5 works
Redis exception read error on connection solution
SAP CR transmission request sequence and dependency check
ALV tree (ll LR RL RR) insert delete
AQS & reentrantlock implementation principle