# 并不对劲的bzoj3994:loj2185:p3327[SDOI2015]约数个数和

##### 题解

$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}$

##### 代码
#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;
}

posted @ 2019-03-06 09:47  echo6342  阅读(106)  评论(0编辑  收藏  举报