首页 技术 正文
技术 2022年11月15日
0 收藏 687 点赞 4,723 浏览 2407 个字
题目大意

设d(x)为x的约数个数,\(t\)组询问,给定\(n,m\)(\(t,m,n\leq5*10^4\)),求$ \sumn_{i=1}\summ_{j=1}d(i*j)$

题解

假设\(n\leq m\)

设\(i=p_1^{a_1}*p_2^{a_2}*…*p_k^{a_k},j=p_1^{b_1}*p_2^{b_2}*…*p_k^{b_k}\)

对于\(i*j\)的某个约数\(x\),设\(x=p_1^{c_1}*p_2^{c_2}*…*p_k^{c_k}\),那么可以用两个数\(e,f\)表示\(x\),当\(c_q\leq a_q\)时\(e\)的\(p_q\)的指数为\(c_q\),当\(c_q> a_q\)时\(f\)的\(p_q\)的指数为\(c_q-a_q\)

这样每个\(x\)都能对应到一对\((e,f)\)上,每对满足\(e|i,f|j,gcd(e,f)=1\)的\((e,f)\)也能对应到一个\(x\)上

所以就有\(d(i,j)=\sum_{e|i}\sum_{f|j}[gcd(e,f)=1]\)

原式=$ \sumn_{i=1}\summ_{j=1}\sum_{e|i}\sum_{f|j}[gcd(e,f)=1]\(
把枚举\)e,f\(放到前面,得原式=\)\sum_{e=1}{n}\sum_{f=1}{m}\lfloor\frac{n}{e}\rfloor\lfloor\frac{m}{f}\rfloor[gcd(e,f)=1]\(
=\)\sum_{e=1}{n}\sum_{f=1}{m}\lfloor\frac{n}{e}\rfloor\lfloor\frac{m}{f}\rfloor\sum_{i|e,i|f}\mu(i)\(
=\)\sum_{i=1}{n}\mu(i)\sum_{i|e}{n}{\lfloor\frac{n}{e}\rfloor}\sum_{i|f}^{m}{\lfloor\frac{m}{f}\rfloor}\(
=\)\sum_{i=1}{n}\mu(i)\sum_{e=1}{\lfloor\frac{n}{i}\rfloor}{\lfloor\frac{n}{ei}\rfloor}\sum_{f=1}^{\lfloor\frac{m}{i}\rfloor}{\lfloor\frac{m}{fi}\rfloor}\(
设\)g(x)=\sum_{i=1}^{x}{\lfloor\frac{x}{i}\rfloor}\(,预处理\)g(x)\(
则原式=\)\sum_{i=1}^{n}{\mu(i)g(n/i)g(m/i)}$

接下来整除分块就行了

代码
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 50010
#define lim 50000
#define LL long long
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(LL x)
{
if(x==0){putchar('0'),putchar('\n');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
return;
}
int n,m,t,p[maxn],no[maxn],cnt;
LL f[maxn],g[maxn],mu[maxn];
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
mu[1]=p[1]=no[1]=1;
rep(i,2,lim)
{
if(!no[i])p[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*p[j]<=lim;j++)
{
no[i*p[j]]=1;
if(i%p[j]==0){mu[i*p[j]]=0;break;}
else mu[i*p[j]]=-mu[i];
}
}
rep(i,1,lim)mu[i]+=mu[i-1];
rep(i,1,lim)
{
for(int l=1,r=0;l<=i;l=r+1)
{
r=i/(i/l);
f[i]+=(LL)(i/l)*(LL)(r-l+1);
}
}
t=read();
while(t--)
{
n=read(),m=read();LL ans=0;
if(n>m)swap(n,m);
for(int l=1,r=0;l<=n;l=r+1)
{
r=min(n/(n/l),m/(m/l));
ans+=(mu[r]-mu[l-1])*f[n/l]*f[m/l];
}
write(ans);
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894