当前位置:网站首页>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
边栏推荐
- SAP debug debug for in, reduce and other complex statements
- GUI, CLI and UNIX Philosophy
- PHP notes (I): development environment configuration
- kettle庖丁解牛第14篇之JSON输入
- kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby
- Number theory blocking (integer division blocking)
- 个人主页软件Fenrus
- 重载、重写、隐藏的对比
- Acquisition of DOM learning elements JS
- Setnx command execution failed due to full redis memory
猜你喜欢

Vivo, hardware safe love and thunder

Comparison of overloading, rewriting and hiding

高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?

Kettle experiment

Kettle experiment (III)

SAP pi / PO function operation status monitoring and inspection

kettle实验

JS DOM event

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

数据清洗 ETL 工具Kettle的安装
随机推荐
Kettle实验 (三)
Redis 内存占满导致的 Setnx 命令执行失败
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
Kettle experiment (III)
PHP notes (I): development environment configuration
数据清洗 ETL 工具Kettle的安装
Go language learning notes - slice, map | go language from scratch
Image processing in opencv -- Introduction to contour + contour features
JS what is an event? Event three elements and operation elements
Simply understand = = and equals, why can string not use new
Summary of wrong questions 1
Vivo, hardware safe love and thunder
ABAP publishes OData service samples from CDs view
MySQL of database -- Fundamentals
《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
SAP debug debug for in, reduce and other complex statements
JS and how to judge custom attributes in H5
[educational codeforces round 80] problem solving Report
Kettle experiment