当前位置:网站首页>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;
}
边栏推荐
- WPF data binding (IV)
- Leetcode-104. Maximum Depth of Binary Tree
- VTK vtkplane and vtkcutter use
- Experience record of rural housing integration script
- Promise. All capture error
- 【迅为干货】龙芯2k1000开发板opencv 测试
- [Xunwei dry goods] opencv test of Godson 2k1000 development board
- 顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
- Stack -- one of two common linear structures of linear structure
- Practice: how to reasonably design functions to solve practical problems in software development (II) -- improving reusability
猜你喜欢

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

The gap between the parent box and the child box

The difference between arrow function and ordinary function

Starting from scratch (V) realize bullet positioning and animation

河南高考VS天津高考(2008年-2021年)

Luogu p1091 chorus formation (longest ascending subsequence)

Education expert wangzhongze shared his experience for many years: family education is not a vassal

Stack -- one of two common linear structures of linear structure

Analysis of key points and difficulties of ES6 promise source code

Deep Attentive Tracking via Reciprocative Learning
随机推荐
Experience record of rural housing integration script
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Leetcode-141. Linked List Cycle
Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
Leetcode-141. Linked List Cycle
Shuttle inside and outside margins
1190. invert the substring between each pair of parentheses
Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
About daily report plan
byte和bit的区别
Outer margin collapse
Latex various arrows / arrows with text labels / variable length arrows
Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
Leetcode hot topic 100 topic 21-25 solution
通过 Ingress 进行灰度发布
模块化笔记
迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)
Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
Listen to the left width of the browser to calculate the distance
一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试