当前位置:网站首页>P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
2022-06-11 07:09:00 【qq_ forty-six million five hundred and eighty thousand two hund】
P3327 [SDOI2015] About a number and
The question :
set up d(x) by x The approximate number of , Given n , m n,m n,m, seek
∑ i = 1 n ∑ j = 1 m d ( i j ) \sum_{i=1}^{n}\sum_{j=1}^{m}d(ij) i=1∑nj=1∑md(ij)
Solution:
First you need to know a derivation :
d ( i j ) = ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = 1 ] d(ij)=\sum_{x|i}\sum_{y|j}[gcd(x,y)=1] d(ij)=x∣i∑y∣j∑[gcd(x,y)=1]
And then we start the derivation of the formula :
∑ i = 1 n ∑ j = 1 m d ( i j ) = ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = 1 ] = ∑ x = 1 n ∑ y = 1 m ⌊ n x ⌋ ⌊ x m ⌋ [ g c d ( x , y ) = 1 ] = ∑ x = 1 n ∑ y = 1 m ⌊ n x ⌋ ⌊ x m ⌋ ∑ d ∣ g c d ( x . y ) μ ( d ) = ∑ d = 1 m i n ( n , m ) μ ( d ) ∑ x = 1 ⌊ n d ⌋ ⌊ n d x ⌋ ∑ y = 1 ⌊ m d ⌋ ⌊ m d y ⌋ \sum_{i=1}^{n}\sum_{j=1}^{m}d(ij) \\ =\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x|i}\sum_{y|j}[gcd(x,y)=1] \\ =\sum_{x=1}^{n}\sum_{y=1}^{m}\lfloor\frac{n}{x}\rfloor\lfloor\frac{x}{m}\rfloor[gcd(x,y)=1] \\ =\sum_{x=1}^{n}\sum_{y=1}^{m}\lfloor\frac{n}{x}\rfloor\lfloor\frac{x}{m}\rfloor\sum_{d|gcd(x.y)}\mu(d) \\ =\sum_{d=1}^{min(n,m)}\mu(d)\sum_{x=1}^{\lfloor\frac{n}{d}\rfloor}\lfloor\frac{n}{dx}\rfloor\sum_{y=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{m}{dy}\rfloor i=1∑nj=1∑md(ij)=i=1∑nj=1∑mx∣i∑y∣j∑[gcd(x,y)=1]=x=1∑ny=1∑m⌊xn⌋⌊mx⌋[gcd(x,y)=1]=x=1∑ny=1∑m⌊xn⌋⌊mx⌋d∣gcd(x.y)∑μ(d)=d=1∑min(n,m)μ(d)x=1∑⌊dn⌋⌊dxn⌋y=1∑⌊dm⌋⌊dym⌋
Here we can see that we can solve it by dividing it into blocks .
Code
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>P;
typedef long long ll;
const int N=5e4;
int mu[N+5],prime[N+5],notPrime[N+5],cnt=0,sumMu[N+5];
ll sum[N+5];
void initMu()
{
mu[1]=1;
for(int i=2;i<=N;i++)
{
if(!notPrime[i])
{
prime[cnt++]=i;
mu[i]=-1;
}
for(int j=0;j<cnt&&1ll*i*prime[j]<=N;j++)
{
notPrime[i*prime[j]]=1;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else break;
}
}
for(int i=1;i<=N;i++)
{
sumMu[i]=sumMu[i-1]+mu[i];
for(int l=1,r;l<=i;l=r+1)
{
r=i/(i/l);
sum[i]+=1ll*i/l*(r-l+1);
}
}
}
int n,m,t;
int main()
{
initMu();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
ll res=0;
int M=min(n,m);
for(int l=1,r;l<=M;l=r+1)
{
r=min(n/(n/l),m/(m/l));
res+=1ll*(sumMu[r]-sumMu[l-1])*sum[n/l]*sum[m/l];
}
printf("%lld\n",res);
}
return 0;
}
边栏推荐
- RGB-D Salient Object Detection withCross-Modality Modulation and Selection
- Heartless sword Chinese English bilingual poem 001 Love
- Notes on learning Es5 and ES6
- Graph Attention Tracking
- Shutter restraint container assembly
- 1190. invert the substring between each pair of parentheses
- Leetcode hot topic 100 topic 6-10 solution
- Comparison of DOM tags of wechat applet development (native and uniapp)
- Esp32 learning notes (49) - esp-wifi-mesh interface use
- Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
猜你喜欢

Listen to the left width of the browser to calculate the distance

Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation

Esp32 learning notes (49) - esp-wifi-mesh interface use

Common troubleshooting tools and analysis artifacts are worth collecting

Stack -- one of two common linear structures of linear structure
![[deploy private warehouse based on harbor] 3 deploy harbor](/img/cd/be68a430e86b4b23ad93b42a338f00.jpg)
[deploy private warehouse based on harbor] 3 deploy harbor

Explain the difference between void 0 and undefined
![Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])](/img/1c/4013479ce1fc5b0ff2ebeb754f05a9.png)
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])

Modular notes

About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally
随机推荐
Experience record of rural housing integration script
【迅为干货】龙芯2k1000开发板opencv 测试
Matplotlib, set coordinate scale size, font / set legend size and font / set vertical and horizontal coordinate name, font and size
Heartless sword Chinese English bilingual poem 001 Love
Promises/a+ standard Chinese Translation
June training (day 11) - matrix
Starting from scratch (V) realize bullet positioning and animation
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
Summary of string processing skills II
SQL query. Only the column name is displayed but not the data
Records how cookies are carried in cross domain requests
Oracle pl/sql these query results cannot be updated. Please include ROWID or use Select For update
Detailed explanation of mutationobserver
matplotlib的cmap
VTK-vtkPlane和vtkCutter使用
Leetcode-141. Linked List Cycle
webserver
Leetcode hot topic 100 topic 11-15 solution
Grayscale publishing through ingress
The difference between TCP and UDP