题目链接

https://lydsy.com/JudgeOnline/problem.php?id=2818

https://lydsy.com/JudgeOnline/problem.php?id=2820

题解

pi=1nj=1m[gcd(i,j)=p]=T=1min(n,m)nTmTpTμ(Tp) \begin{aligned} & \sum_{p} \sum_{i=1}^n\sum_{j=1}^m [\gcd(i,j)=p]\\ = & \sum_{T=1}^{\min(n,m)}\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum_{p|T}\mu(\frac{T}{p}) \end{aligned}

可以整除分块。

考虑
g(T)=pTμ(Tp) g(T)=\sum_{p|T}\mu(\frac{T}{p})

g(T)={0T=11TPg(pT)={μ(T)pP,pTμ(T)g(T)pP,pT g(T)=\begin{cases} 0 & T=1\\ 1 & T\in \mathbb P \end{cases}\\ g(pT)=\begin{cases} \mu(T) & p\in \mathbb P,p \mid T\\ \mu(T)-g(T) & p\in \mathbb P,p\nmid T \end{cases}
因此可以通过线筛求gg

代码

BZOJ 2818

#include <cstdio>

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

const int maxn=10000000;

int p[maxn+10],prime[maxn+10],cnt,mu[maxn+10];
long long f[maxn+10];

int getprime()
{
  p[1]=mu[1]=1;
  f[1]=0;
  for(int i=2; i<=maxn; ++i)
    {
      if(!p[i])
        {
          prime[++cnt]=i;
          mu[i]=-1;
          f[i]=1;
        }
      for(int j=1; (j<=cnt)&&(i*prime[j]<=maxn); ++j)
        {
          int x=i*prime[j];
          p[x]=1;
          if(i%prime[j]==0)
            {
              mu[x]=0;
              f[x]=mu[i];
              break;
            }
          mu[x]=-mu[i];
          f[x]=-f[i]+mu[i];
        }
    }
  for(int i=2; i<=maxn; ++i)
    {
      f[i]+=f[i-1];
    }
  return 0;
}

int n;

int main()
{
  getprime();
  n=read();
  long long ans=0;
  for(int l=1,r; l<=n; l=r+1)
    {
      r=n/(n/l);
      ans+=(f[r]-f[l-1])*(n/l)*(n/l);
    }
  printf("%lld\n",ans);
  return 0;
}

BZOJ 2820

#include <cstdio>
#include <algorithm>

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

const int maxn=10000000;

int p[maxn+10],prime[maxn+10],cnt,mu[maxn+10];
long long f[maxn+10];

int getprime()
{
  p[1]=mu[1]=1;
  f[1]=0;
  for(int i=2; i<=maxn; ++i)
    {
      if(!p[i])
        {
          prime[++cnt]=i;
          mu[i]=-1;
          f[i]=1;
        }
      for(int j=1; (j<=cnt)&&(i*prime[j]<=maxn); ++j)
        {
          int x=i*prime[j];
          p[x]=1;
          if(i%prime[j]==0)
            {
              mu[x]=0;
              f[x]=mu[i];
              break;
            }
          mu[x]=-mu[i];
          f[x]=-f[i]+mu[i];
        }
    }
  for(int i=2; i<=maxn; ++i)
    {
      f[i]+=f[i-1];
    }
  return 0;
}

int T,n,m;

int main()
{
  getprime();
  T=read();
  while(T--)
    {
      n=read();
      m=read();
      long long ans=0;
      for(int l=1,r; (l<=n)&&(l<=m); l=r+1)
        {
          r=std::min(n/(n/l),m/(m/l));
          ans+=(f[r]-f[l-1])*(n/l)*(m/l);
        }
      printf("%lld\n",ans);
    }
  return 0;
}