【学习笔记】数学知识-质数

前置知识

  • 质数的个数是无限的。
  • \(n\) 为正整数,有 \(n!\) 的所有质因子不超过 \(n\)
    • 证明:对于所有的 \(d \in \mathbb{P},d|n!\) ,一定有 \(\sum\limits_{i=1}^{n}[d|i]>0\) ,易证 \(d \le n\)
  • 一个合数 \(n\) 一定包含一个不超过 \(\sqrt{n}\) 的质因子。
  • 试除法:若一个正整数 \(n\) 为合数,则存在一个能整除 \(n\) 的数 \(d\) ,其中 \(2 \le d \le \sqrt{n},d \in \mathbb{P}\)
    • 时间复杂度为 \(O(\sqrt{n})\)

    • 代码实现

      点击查看代码
      bool isprime(int n)
      {
          if (n < 2)
              return false;
          for (int i = 2; i <= sqrt(n); i++)
              if (n % i == 0)
                  return false;
          return true;
      }
      

筛法

  • Eratosthenes 筛法(埃式筛法)
    • 时间复杂度为 \(O(n \times \log \log n)\)

    • luogu P3912 素数个数

      点击查看代码
      bool m[100000010];
      int main()
      {
          int n,i,j,ans=0;
          cin>>n;
          m[1]=1;
          for(i=2;i<=sqrt(n);i++)
          {
              if(m[i]==0)
              {
                  for(j=2;i*j<=n;j++)
                  {
                      m[i*j]=1;
                  }
              }
          }
          for(i=2;i<=n;i++)
          {
              if(m[i]==0)
              {
                  ans++;
              }
          }
          cout<<ans;
          return 0;
      }
      
  • 线性筛法(欧拉筛法)
    • 时间复杂度为 \(O(n)\)

    • luogu P3383 【模板】线性筛素数

      点击查看代码
      int prime[100000001],len=0;
      bool vis[100000001];
      void isprime(int n)
      {
          int i,j;
          memset(vis,false,sizeof(vis));
          for(i=2;i<=n;i++)
          {
              if(vis[i]==false)
              {
                  len++;
                  prime[len]=i;
              }
              for(j=1;j<=len&&i*prime[j]<=n;j++)
              {
                  vis[i*prime[j]]=true;
                  if(i%prime[j]==0)
                  {
                      break;
                  }
              }
          }
      }
      int main()
      {
          int n,q,i,k;
          cin>>n>>q;
          isprime(n);
          for(i=1;i<=q;i++)
          {
              cin>>k;
              cout<<prime[k]<<endl;
          }
          return 0;
      }
      
  • 例题
    • luogu P1835 素数密度
      • 直接对 \(1\sim r\) 进行线性筛显然不可做。

      • 筛法求出 \(2\sim \sqrt{r}\) 之间的质数,对于每个质数 \(p\) ,把 \([l,r]\) 中能被 \(p\) 整除的数标记,即标记 \(i\times p(\max(2,\left\lceil\dfrac{l}{p}\right\rceil) \le i \le \left\lceil\dfrac{r}{p}\right\rceil)\) 为合数。

        点击查看代码
        ll prime[70000],len=0;
        bool vis[70000];
        bitset<2000001>dis;//bitset大法好
        void isprime(ll n)
        {
            memset(vis,false,sizeof(vis));
            for(ll i=2;i<=n;i++)
            {
                if(vis[i]==false)
                {
                    len++;
                    prime[len]=i;
                }
                for(ll j=1;j<=len&&i*prime[j]<=n;j++)
                {
                    vis[i*prime[j]]=true;
                    if(i%prime[j]==0)
                    {
                        break;
                    }
                }
            }
        }
        int main()
        {
            ll l,r,i,j,ans=0;
            cin>>l>>r;
            isprime(70000);
            if(l==1)
            {
                l=2;
            }
            for(i=1;i<=len;i++)
            {
                for(j=max(2ll,(ll)ceil(1.0*l/prime[i]));j<=(ll)ceil(1.0*r/prime[i]);j++)
                {
                    dis[j*prime[i]-l]=1;
                }
            }
            for(i=l;i<=r;i++)
            {
                if(dis[i-l]==0)
                {
                    ans++;
                }
            }
            cout<<ans<<endl;
            return 0;
        }
        
    • UVA10140 Prime Distance
    • luogu P10495 阶乘分解

算术基本定理(唯一分解定理)

  • 任何一个大于 \(1\) 的正整数都能唯一分解成有限个的质数的乘积,即若 \(n\) 为大于 \(1\) 的正整数,则有 \(n=\prod\limits_{i=1}^{m}p_i^{c_i}\) ,其中对于每一个 \(i(1 \le i \le m)\) 均满足 \(c_i\) 为正整数, \(p_i\in \mathbb{P}\) ,且 \(p_1<p_2< \dots <p_m\)
  • 应用
    • \(n\) 为大于 \(1\) 的正整数,设 \(n=\prod\limits_{i=1}^{m}p_i^{c_i}\) ,则有如下性质:
      • \(n^2=\prod\limits_{i=1}^{m}p_i^{2c_i}\)
      • \(n\) 的正约数集合可写作 \(\{ \prod\limits_{i=1}^{m}p_i^{b_i} \}\) ,其中对于每一个 \(i(1 \le i \le m)\) 均有 \(0 \le b_i \le c_i\)
      • \(\sum\limits_{d=1}^{n}[d|n]=\prod\limits_{i=1}^{m}(\sum\limits_{j=0}^{c_i}1^j)=(c_1+1)(c_2+1) \dots (c_m+1)=\prod\limits_{i=1}^{m}(c_i+1)\)
      • \(\sum\limits_{d|n^2}^{} d^2 \le (\sum\limits_{d|n}^{} d^2)^2\)
        • \(\sum\limits_{d|n}^{} d=(p_1^0+p_1^1+ \dots +p_1^{c_1})(p_2^0+p_2^1+ \dots +p_2^{c_2}) \dots (p_m^0+p_m^1+ \dots +p_m^{c_m})=\prod\limits_{i=1}^{m}(\sum\limits_{j=0}^{c_i}p_i^j)\)
        • \(\sum\limits_{d|n}^{} d^2=\prod\limits_{i=1}^{m}(\sum\limits_{j=0}^{c_i}(p_i^j)^2)=\prod\limits_{i=1}^{m}(\sum\limits_{j=0}^{c_i}p_i^{2j})\)
          • \(\sum\limits_{d|n^2}^{} d^2=\prod\limits_{i=1}^{m}(\sum\limits_{j=0}^{2c_i}p_i^{2j})\)
        • \((\sum\limits_{d|n}^{} d^2)^2=\prod\limits_{i=1}^{m}((\sum\limits_{j=0}^{c_i}p_i^{2j})^2)=\prod\limits_{i=1}^{m}(\sum\limits_{j=0}^{2c_i}(\min(j,2c_i-j)+1) \times p_i^{2j})\)
    • 例题
      • CF1444A Division
        • 简化题意
          • \(t\) 组数据,每组数据给定 \(P,Q\) ,求满足 \(x|P,Q \nmid x\) 的最大正整数解。
        • 解法
          • \(Q \nmid P\) 时,有 \(x=P\) 是一组最大的解。
          • \(Q|P\) 时,设 \(Q=\prod\limits_{i=1}^{m}p_i^{c_i},c'_i\) 是满足 \(p_i^{c'_i}|P\) 的最大正整数, \(c''_i\) 是满足 \(p_i^{c''_i}|x\) 的最大正整数,由 \(Q \nmid x\) 有存在一个 \(i(1 \le i \le m)\) 满足 \(c_i>c''_i\) ,构造一个 \(i\) 满足 \(c''_i=c_i-1\) 即可。故有 \(x=\max\limits_{i=1}^{m} \{ \dfrac{P}{p_i^{c'_i}} \times p_i^{c_i-1} \}\) 是一组最大的解。

互质

  • \(\forall a,b \in \mathbb{N},\gcd(a,b)=1\) ,则称 \(a,b\) 互质。
  • 对于三个数或更多个数的情况,将 \(\gcd(a,b,c)=1\) 的情况称为 \(a,b,c\) 互质;将 \(\gcd(a,b)=\gcd(a,c)=\gcd(b,c)=1\) 的情况称为 \(a,b,c\) 两两互质。

欧拉函数

  • \(1 \sim n\) 中与 \(n\) 互质的数的个数被称为欧拉函数,记作 \(\varphi(n)\)
  • \(\varphi(n)=\sum\limits_{i=1}^{n}[\gcd(i,n)=1]=\begin{cases}1 & n= 1 \\ n \times \prod \limits_{p \in \mathbb{P},p|n}{}(1-\dfrac{1}{p}) & n \ne 1\end{cases}\)
    • 证明
      • \(n=1\) 时,显然。
      • \(n \ne 1\) 时,设 \(p,q(p \ne q)\)\(n\) 的质因子, \(1 \sim n\)\(p\) 的倍数共有 \(\dfrac{n}{p}\) 个, \(q\) 的倍数共有 \(\dfrac{n}{q}\) 个,依据容斥原理, \(1 \sim n\) 中不与 \(n\) 含有共同质因子 \(p\)\(q\) 的个数为 \(n -\dfrac{n}{p}-\dfrac{n}{q}+\dfrac{n}{p \times q}= n \times (1- \dfrac{1}{p}- \dfrac{1}{q}+ \dfrac{1}{p \times q})= n \times (1- \dfrac{1}{p}) \times (1- \dfrac{1}{q})\) 。类似地,可在 \(n\) 的全部质因子上使用容斥原理,即可得到与 \(n\) 互质的数的个数。
  • 性质
    • \(p\) 为质数,则 \(\varphi(p)=p-1,\varphi(p^k)=p^k-p^{k-1}=(p-1) \times p^{k-1}\)

      • 证明:设 \(n=p^k\) ,比 \(n\) 小的正整数有 \(p^k-1\) 个。其中共有 \(p^{k-1}-1\) 个数能被 \(p\) 整除,即这些数不与 \(p^k\) 互质。故 \(\varphi(p^k)=p^k-1-(p^{k-1}-1)=p^k-p^{k-1}=(p-1) \times p^{k-1}\)
    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n}i \times [\gcd(i,n)=1]=\begin{cases}1 & n= 1 \\ \dfrac{n \times \varphi(n)}{2} & n \ne 1\end{cases}\)

      • 证明
        • \(n=1\) 时,显然。
        • \(n \ne 1\) 时,依据更相减损法,有 \(\gcd(n,x)=\gcd(n,n-x)\) ,即与 \(n\) 互质的数 \(x,n-x\) 成对出现,平均值为 \(\dfrac{n}{2}\)
    • \(2<n\) ,则 \(2|\varphi(n)\)

    • \(n\) 为偶数,有 \(\varphi(n) \le \dfrac{n}{2}\)

    • \(n\) 为奇数,则 \(\varphi(2n)=\varphi(2) \times \varphi(n)=\varphi(n)\)

    • \(a,b\) 互质,则 \(\varphi(ab)=\varphi(a) \times \varphi(b)\)

      • 证明:依据算术基本定理,设 \(a=\prod\limits_{i=1}^n p_i^{c_i},b=\prod\limits_{i=1}^m q_i^{c'_i}\) ,又因为 \(a,b\) 互质,所以不存在一组 \(i,j\) 满足 \(p_i=q_j\) ,故 \(\varphi(ab)=ab \times \prod\limits_{i=1}^n (1-\dfrac{1}{p_i}) \times \prod\limits_{i=1}^m (1-\dfrac{1}{q_i})\) ,又有 \(\varphi(a)=a \times \prod\limits_{i=1}^n (1-\dfrac{1}{p_i}),\varphi(b)=b \times \prod\limits_{i=1}^m (1-\dfrac{1}{q_i})\) ,故 \(\varphi(ab)=\varphi(a) \times \varphi(b)\)
        • 由本条性质可知欧拉函数是积性函数。
    • \(a|b\) ,则 \(\varphi(ab)=a \times \varphi(b),\varphi(a) | \varphi(b)\)

      • 证明:因为 \(ab\)\(b\) 所有的质因子是相同的,只是部分质因子的指数发生变化,故 \(\varphi(ab)=a \times \varphi(b)\)
    • \(p\) 为质数,且 \(p|n,p^2|n\) ,则 \(\varphi(n)=\varphi(\dfrac{n}{p}) \times p\)

      • 证明:因为 \(p|n,p^2|n\) ,所以 \(n,\dfrac{n}{p}\) 有相同的质因子(其中 \(p\) 的指数不同),依据欧拉函数的计算公式,此时有 \(\dfrac{\varphi(n)}{\varphi(\dfrac{n}{p})}=p\) ,故 \(\varphi(n)=\varphi(\dfrac{n}{p}) \times p\)
    • \(p\) 为质数,且 \(p|n,p^2 \nmid n\) ,则 \(\varphi(n)=\varphi(\dfrac{n}{p}) \times (p-1)\)

      • 证明:因为 \(p|n,p^2 \nmid n\) ,所以 \(p,\dfrac{n}{p}\) 互质,又因为 \(\varphi\) 是积性函数,故 \(\varphi(n)=\varphi(\dfrac{n}{p}) \times \varphi(p)=\varphi(\dfrac{n}{p}) \times (p-1)\)
    • 欧拉反演:\(n=\sum\limits_{d|n}^{}\varphi(d)\)

      • 证明:设 \(k\) 满足 \(k<n,\gcd(k,n)=d\) ,则 \(\gcd(\dfrac{k}{d},\dfrac{n}{d})=1\) ;设 \(f(x)\) 表示满足 \(\gcd(k,n)=x\)\(k\) 的个数,则 \(n=\sum\limits_{i=1}^n f(i)\) 。又因为 \(\dfrac{k}{x}\)\(\dfrac{n}{x}\) 互质,有 \(f(x)=\varphi(\dfrac{n}{x})\) ,则 \(n=\sum\limits_{i|n}^{}\varphi(\dfrac{n}{i})\) 。易知 \(i\)\(\dfrac{n}{i}\) 是一一对应的,故 \(n=\sum\limits_{d|n}^{}\varphi(d)\)
    • 依据算术基本定理,有 \(n=\prod\limits_{i=1}^m p_i^{c_i}\) ,又因为 \(\varphi\) 是积性函数,则 \(\varphi(n)=\prod\limits_{i=1}^m \varphi(p_i^{c_i})=\prod\limits_{i=1}^m (p_i-1) \times p_i^{{c_i}-1}=\prod\limits_{i=1}^m (1-\dfrac{1}{p_i}) \times p_i^{c_i}=n \times \prod\limits_{i=1}^m (1-\dfrac{1}{p_i})=n \times \prod\limits_{i=1}^m \dfrac{p_i-1}{p_i}\)

      • PS:本条性质用于已知 \(\varphi\) 是积性函数求 \(\varphi\) 的计算式,可作了解。
    • \(n,m\) 为正整数,且满足 \(1 \le m<n\) ,有 \(\sum\limits_{i=0}^{n-1}[\gcd(m,n)=\gcd(m+i,n)]= \varphi(\dfrac{n}{\gcd(n,m)})\)

      • CF1295D Same GCDs

      • 证明

        首先显然有 \(\sum\limits_{i=0}^{n-1}((m+i) \bmod n)=\sum\limits_{i=0}^{n-1}(i \bmod n)\)

        \(\begin{aligned}\sum\limits_{i=0}^{n-1}[\gcd(m,n)=\gcd(m+i,n)] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=0}^{n-1}[\gcd(m,n)=\gcd((m+i) \bmod n,n)] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=0}^{n-1}[\gcd(m,n)=\gcd(i,n)] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n-1}[\gcd(m,n)=\gcd(i,n)] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n}[\gcd(m,n)=\gcd(i,n)] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{ \frac{n}{\gcd(n,m)}}[\gcd(i,\dfrac{n}{\gcd(n,m)})=1] \end{aligned}\)

        \(\begin{aligned} &=\varphi(\dfrac{n}{\gcd(n,m)}) \end{aligned}\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \gcd(i,n)=\sum\limits_{d|n}^{} d \times \varphi({\dfrac{n}{d}})=\sum\limits_{d|n}^{} \varphi(d) \times \dfrac{n}{d}\)

      • luogu P2303 [SDOI2012] Longge 的问题

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n} \gcd(i,n) \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d|n}^{} d \sum\limits_{i=1}^{n} [\gcd(i,n)=d] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d|n}^{} d \sum\limits_{i=1}^{\frac{n}{d}} [\gcd(i,\dfrac{n}{d})=1] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d|n}^{} d \times \varphi({\dfrac{n}{d}})\end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d|n}^{} \varphi(d) \times \dfrac{n}{d}\end{aligned}\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \operatorname{lcm}(i,n)=\dfrac{n(1+\sum\limits_{d|n}^{} d \times \varphi({d}))}{2}\)

      • luogu P1891 疯狂 LCM | SP5971 LCMSUM - LCM Sum

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n} \operatorname{lcm}(i,n) \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \dfrac{n \times i}{\gcd(i,n)}\end{aligned}\)

        \(\begin{aligned} &=n \sum\limits_{i=1}^{n} \dfrac{i}{\gcd(i,n)}\end{aligned}\)

        \(\begin{aligned} &=n \sum\limits_{d|n}^{}\sum\limits_{i=1}^{n} \frac{i}{d} \times [\gcd(i,n)=d]\end{aligned}\)

        \(\begin{aligned} &=n \sum\limits_{d|n}^{}\sum\limits_{i=1}^{\frac{n}{d}} i \times [\gcd(i,\dfrac{n}{d})=1]\end{aligned}\)

        \(\begin{aligned} &=n(\dfrac{1}{2}+\sum\limits_{d|n}^{}\dfrac{ \dfrac{n}{d} \times \varphi(\dfrac{n}{d})}{2})\end{aligned}\)

        \(\begin{aligned} &=n(\dfrac{1}{2}+\sum\limits_{d|n}^{}\dfrac{d \times \varphi(d)}{2})\end{aligned}\)

        \(\begin{aligned} &=\dfrac{n(1+\sum\limits_{d|n}^{} d \times \varphi({d}))}{2}\end{aligned}\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j)=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{i-1} \gcd(i,j)+\sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} \gcd(i,j)+\sum\limits_{i=1}^{n}\gcd(i,i)\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{i-1} \gcd(i,j)=\sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} \gcd(i,j)=\dfrac{-\dfrac{n(n+1)}{2}+\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j)}{2}\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} [\gcd(i,j)=1]=1+2 \sum\limits_{i=2}^{n}\varphi(i)=-1+2 \sum\limits_{i=1}^{n} \varphi(i)\)

      • UVA10820 交表 Send a Table | luogu P2158 [SDOI2008] 仪仗队

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} [\gcd(i,j)=1] \end{aligned}\)

        \(\begin{aligned} &=(\sum\limits_{i=2}^{n}2\sum\limits_{j=2}^{i-1} [\gcd(i,j)=1])+(\sum\limits_{i=1}^{n} [\gcd(i,i)=1]) \end{aligned}\)

        \(\begin{aligned} &=(\sum\limits_{i=2}^{n}2\sum\limits_{j=1}^{i-1} [\gcd(i,j)=1])+1 \end{aligned}\)

        \(\begin{aligned} &=(\sum\limits_{i=2}^{n}2\sum\limits_{j=1}^{i} [\gcd(i,j)=1])+1 \end{aligned}\)

        \(\begin{aligned} &=1+2 \sum\limits_{i=2}^{n} \varphi(i) \end{aligned}\)

        \(\begin{aligned} &=-1+2 \sum\limits_{i=1}^{n} \varphi(i)\end{aligned}\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j)=\sum\limits_{d=1}^{n} \varphi(d) \left\lfloor\dfrac{n}{d}\right\rfloor^2\)

      • luogu P2398 GCD SUM

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j) \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d| \gcd(i,j)}^{} \varphi(d) \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d|i,d|j}^{} \varphi(d) \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d=1}^{n} \varphi(d) \sum\limits_{i=1}^{n} [d|i] \sum\limits_{j=1}^{n} [d|j] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d=1}^{n} \varphi(d) \left\lfloor\dfrac{n}{d}\right\rfloor \left\lfloor\dfrac{n}{d}\right\rfloor \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d=1}^{n} \varphi(d) \left\lfloor\dfrac{n}{d}\right\rfloor^2 \end{aligned}\)

      • 拓展:若 \(n,m\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m} \gcd(i,j)=\sum\limits_{d=1}^{\min(n,m)} \varphi(d) \left\lfloor\dfrac{n}{d}\right\rfloor \left\lfloor\dfrac{m}{d}\right\rfloor\)

        • luogu P1447 [NOI2010] 能量采集

        • 证明

          \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m} \gcd(i,j) \end{aligned}\)

          \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m} \sum\limits_{d| \gcd(i,j)}^{} \varphi(d) \end{aligned}\)

          \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m} \sum\limits_{d|i,d|j}^{} \varphi(d) \end{aligned}\)

          \(\begin{aligned} &=\sum\limits_{d=1}^{\min(n,m)} \varphi(d) \sum\limits_{i=1}^{n} [d|i] \sum\limits_{j=1}^{m} [d|j] \end{aligned}\)

          \(\begin{aligned} &=\sum\limits_{d=1}^{\min(n,m)} \varphi(d) \left\lfloor\dfrac{n}{d}\right\rfloor \left\lfloor\dfrac{m}{d}\right\rfloor \end{aligned}\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j)= \sum\limits_{d=1}^{n}d (-1+2 \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i))\)

      • luogu P2398 GCD SUM

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j) \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d=1}^{n}d [\gcd(i,j)=d] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d=1}^{n}d [\gcd(\dfrac{i}{d},\dfrac{j}{d})=1] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d=1}^{n}d \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \sum\limits_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor} [\gcd(i,j)=1] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d=1}^{n}d (-1+2 \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i))\end{aligned}\)

      • 拓展:若 \(n,m\) 为正整数,则有 link(太长就放这里了)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j)^{2}= \sum\limits_{d=1}^{n}d^{2} (-1+2 \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i))\)

      • luogu P8670 [蓝桥杯 2018 国 B] 矩阵求和

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd(i,j)^{2} \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d=1}^{n}d^{2} [\gcd(i,j)=d] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d=1}^{n}d^{2} [\gcd(\dfrac{i}{d},\dfrac{j}{d})=1] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d=1}^{n}d^{2} \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \sum\limits_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor} [\gcd(i,j)=1] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d=1}^{n}d^{2} (-1+2 \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i))\end{aligned}\)

    • \(n\) 为正整数,则 \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} [\gcd(i,j) \in \mathbb{P}]= \sum\limits_{d \in \mathbb{P}}^{n} (-1+2 \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i))\)

      • luogu P2568 GCD

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} [\gcd(i,j) \in \mathbb{P}] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d\in \mathbb{P}}^{n}[\gcd(i,j)=d] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \sum\limits_{d\in \mathbb{P}}^{n} [\gcd(\dfrac{i}{d},\dfrac{j}{d})=1] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d\in \mathbb{P}}^{n} \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \sum\limits_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor} [\gcd(i,j)=1] \end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{d \in \mathbb{P}}^{n}(-1+2 \sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i)) \end{aligned}\)

    • \(n,m\) 均为正整数,且 \(m \le n\) ,有 \(\sum\limits_{i=1}^{n!}[\gcd(i,m!)=1]=n! \times \dfrac{\prod\limits_{p \in \mathbb{P}}^{m}(p-1)}{\prod\limits_{p \in \mathbb{P}}^{m}p}\)

      • luogu P2155 [SDOI2008] 沙拉公主的困惑

      • 证明

        \(\begin{aligned}\sum\limits_{i=1}^{n!}[\gcd(i,m!)=1]\end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n!}[\gcd(i \bmod m!,m!)=1]\end{aligned}\)

        \(\begin{aligned} &=\dfrac{n!}{m!} \times \sum\limits_{i=0}^{m!-1}[\gcd(i,m!)=1]\end{aligned}\)

        \(\begin{aligned} &=\dfrac{n!}{m!} \times \sum\limits_{i=1}^{m!}[\gcd(i,m!)=1]\end{aligned}\)

        \(\begin{aligned} &=\dfrac{n!}{m!} \times \varphi(m!)\end{aligned}\)

        \(\begin{aligned} &=\dfrac{n!}{m!} \times m! \times \prod\limits_{p \in \mathbb{P},p|m!}^{} \dfrac{p-1}{p} \end{aligned}\)

        \(\begin{aligned} &=n! \times \prod\limits_{p \in \mathbb{P},p|m!}^{} \dfrac{p-1}{p} \end{aligned}\)

        \(\begin{aligned} &=n! \times \prod\limits_{p \in \mathbb{P}}^{m} \dfrac{p-1}{p} \end{aligned}\)

        \(\begin{aligned} &=n! \times \dfrac{\prod\limits_{p \in \mathbb{P}}^{m}(p-1)}{\prod\limits_{p \in \mathbb{P}}^{m}p} \end{aligned}\)

    • \(n\) 为正整数,有 \(\sum\limits_{i=1}^{n}\sum\limits_{j=i}^{n}[\gcd(i,j)>1]=\sum\limits_{i=1}^{n}i-\sum\limits_{i=1}^{n}\varphi(i)=\sum\limits_{i=1}^{n}(i-\varphi(i))\)

      • SP21615 NAJPWG - Playing with GCD

      • 证明

        \(\begin{aligned} \sum\limits_{i=1}^{n}\sum\limits_{j=i}^{n}[\gcd(i,j)>1]\end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{j=1}^{n}\sum\limits_{i=1}^{j}[\gcd(i,j)>1]\end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}[\gcd(i,j)>1]\end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}[\gcd(i,j) \ge 1]-\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}[\gcd(i,j)=1]\end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n}i-\sum\limits_{i=1}^{n}\varphi(i)\end{aligned}\)

        \(\begin{aligned} &=\sum\limits_{i=1}^{n}(i-\varphi(i))\end{aligned}\)

  • 例题
    • SP4141 ETF - Euler Totient Function | UVA10179 Irreducable Basic Fractions | UVA10299 Relatives
      • 对原数进行 分解质因数 ,顺便求出欧拉函数。

        点击查看代码
        int phi(int n)
        {
        	int ans=n,i;
        	for(i=2;i<=sqrt(n);i++)
        	{
        		if(n%i==0)
        		{
        			ans=ans/i*(i-1);
        			while(n%i==0)
        			{
        				n/=i;
        			}
        		}
        	}
        	if(n>1)
        	{
        		ans=ans/n*(n-1);
        	}
        	return ans;
        }
        
    • UVA11327 Enumerating Rational Numbers
      • 线性筛欧拉函数板子

        • 线性筛欧拉函数的时间复杂度为 \(O(n)\)
        点击查看代码
        void euler(ll n)
        {
        	memset(vis,false,sizeof(vis));
        	phi[1]=1;
        	for(ll i=2;i<=n;i++)
        	{
        		if(vis[i]==false)
        		{
        			len++;
        			prime[len]=i;
        			phi[i]=i-1;//phi[i]表示i的欧拉函数
        		}
        		for(ll j=1;j<=len&&i*prime[j]<=n;j++)
        		{
        			vis[i*prime[j]]=true;
        			if(i%prime[j]==0)
        			{
        				phi[i*prime[j]]=phi[i]*prime[j];
        				break;
        			}
        			else
        			{
        				phi[i*prime[j]]=phi[i]*(prime[j]-1);
        			}
        		}
        	}
        }
        

积性函数

  • 若函数 \(f\) 满足 \(f(1)=1\)\(\forall a,b \in \mathbb{N}^{*},\gcd(a,b)=1\) 均有 \(f(ab)=f(a) \times f(b)\) ,那么称函数 \(f\) 为积性函数。
  • 若函数 \(f\) 满足 \(f(1)=1\)\(\forall a,b \in \mathbb{N}^{*}\) 均有 \(f(ab)=f(a) \times f(b)\) ,那么称函数 \(f\) 为完全积性函数。
  • 性质
    • 若函数 \(f\) 是积性函数,依据算术基本定理,有 \(n=\prod\limits_{i=1}^m p_i^{c_i}\) ,则 \(f(n)=\prod\limits_{i=1}^m f(p_i^{c_i})\) ;若函数 \(f\) 是完全积性函数,依据算术基本定理,有 \(n=\prod\limits_{i=1}^m p_i^{c_i}\) ,则 \(f(n)=\prod\limits_{i=1}^m f(p_i)^{c_i}\)
    • \(f,g\) 均为积性函数,\(\begin{cases} h(n)=f(n^{k}) \\ h(n)=f^{k}(n) \\ h(n)=f(n)g(n) \\ h(n)=\sum\limits_{d|n}f(d)g(\frac{n}{d}) \end{cases}\) 均是积性函数。
  • 常见的积性函数
    • 单位函数(完全积性): \(\varepsilon(n)=[n=1]\)
    • 恒等函数(完全积性): \(\operatorname{id}_{k}(n)=n^{k}\)\(\operatorname{id}_{1}(n)\) 常简记作 \(\operatorname{id}_{1}(n)\)
    • 常数函数(完全积性): \(1(n)=1\)
    • 除数函数: \(\sigma_{k}(n)=\sum\limits_{d|n}d^{k}\)\(\sigma_{0}(n)\) 常简记作 \(d(n)\)\(\tau(n)\)\(\sigma_{1}(n)\) 常简记作 \(\sigma(n)\)
    • 欧拉函数: \(\varphi(n)=\sum\limits_{i=1}^{n}[\gcd(i,n)=1]\)
    • 莫比乌斯函数: \(\mu(n)=\begin{cases} 1 & n=1 \\ 0 & n \ 有平方质因子 \\ (-1)^{k} & k \ 为 \ n \ 的本质不同质因子个数 \end{cases}\)
  • 线性筛任意积性函数
    • \(v_p(i)\) 表示 \(i\) 的最小质因子 \(p\) 的最高次幂的次数,即 \(p^{v_p(i)}|i,p^{v_p(i)+1} \nmid i\) 。预处理 \(low_i\) 表示 \(i\) 的最小质因子 \(p\) 的最高次幂,即 \(low_i=p^{v_p(i)}\)
    • 在线性筛的过程中,需要对 \(f_1\) 进行边界处理。当 \(i\) 是素数时,需要对 \(f_i\) 进行预值处理。
      • \(\gcd(i,prime_j)=1\) 时,因为 \(i\) 的所有质因子都大于 \(prime_j\),所以 \(low_{i \times prime_j}=prime_j,f_{i \times prime_j}=f_i \times f_{prime_j}\)

      • 否则,有 \(low_{i \times prime_j}=low_i \times prime_j\)

        • \(i=low_i\) ,即 \(i\) 仅有一个质因子时,此时 \(f_{i \times prime_j}\) 需要根据实际处理。
        • \(i \ne low_i\) ,即 \(i\) 不仅有一个质因子时,因为 \(\gcd(\dfrac{i}{low_i},low_{i \times prime_j})=1\) ,所以 \(f_{i \times prime_j}=f_{\frac{i}{low_i}} \times f_{low_{i \times prime_j}}\)
        点击查看代码
        ll val(ll i ,ll prime)
        {
            if(i==prime)
            {
                return ...;
            }
            else
            {
                return ...;
            }
        }
        void euler(ll n,ll val0)
        {
            memset(vis,false,sizeof(vis));
            f[1]=val0;//边界处理
            for(ll i=2;i<=n;i++)
            {
                if(vis[i]==false)
                {
                    len++;
                    prime[len]=i;
                    f[i]=val(i,i);//当i是素数时,预值处理
                    low[i]=i;
                }
                for(ll j=1;j<=len&&i*prime[j]<=n;j++)
                {
                    vis[i*prime[j]]=true;
                    if(i%prime[j]==0)
                    {
                        low[i*prime[j]]=low[i]*prime[j];
                        if(i==low[i])
                        {
                            f[i*prime[j]]=val(i,prime[j]);//根据实际处理
                        }
                        else
                        {	
                            f[i*prime[j]]=f[i/low[i]]*f[low[i*prime[j]]];
                        }
                        break;
                    }
                    else
                    {
                        low[i*prime[j]]=prime[j];
                        f[i*prime[j]]=f[i]*f[prime[j]];
                    }
                }
            }
        }
        
    • 应用
      • 线性筛预处理 \(f(n)=\sum\limits_{d|n}^{} d \times \varphi({d})\)

        点击查看代码
        void euler(ll n)
        {
        	memset(vis,false,sizeof(vis));
        	f[1]=1;
        	for(ll i=2;i<=n;i++)
        	{
        		if(vis[i]==false)
        		{
        			len++;
        			prime[len]=i;
        			f[i]=1+i*(i-1);
        			low[i]=i;
        		}
        		for(ll j=1;j<=len&&i*prime[j]<=n;j++)
        		{
        			vis[i*prime[j]]=true;
        			if(i%prime[j]==0)
        			{
        				low[i*prime[j]]=low[i]*prime[j];
        				if(i==low[i])
        				{
        					f[i*prime[j]]=f[i]+i*prime[j]*i*(prime[j]-1);
        				}
        				else
        				{	
        					f[i*prime[j]]=f[i/low[i]]*f[low[i*prime[j]]];
        				}
        				break;
        			}
        			else
        			{
        				low[i*prime[j]]=prime[j];
        				f[i*prime[j]]=f[i]*f[prime[j]];
        			}
        		}
        	}
        }
        

莫比乌斯函数

posted @ 2023-08-18 16:49  hzoi_Shadow  阅读(93)  评论(3编辑  收藏  举报
扩大
缩小