• 莫比乌斯反演笔记


    已知:$sum_{t|n}mu (t)=[n=1]$


    一,求${sum_{i=1}^{n}sum_{j=1}^{m}[gcd(i,j)=1]}$

      其中$nleq 1e7,mleq 1e7$

      原式${=sum_{i=1}^{n}sum_{j=1}^{m} sum_{t|i,t|j}mu (t)}$

        ${=sum_{t=1}^{n}left lfloor frac{n}{t} ight floorleft lfloor frac{m}{t} ight floormu (t)}$

      线性筛莫比乌斯函数即可

      

      

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<vector>
     5 #include<cstdlib>
     6 #include<cmath>
     7 #include<cstring>
     8 using namespace std;
     9 #define maxn 10010
    10 #define llg long long
    11 #define N 2010 
    12 #define yyj() freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
    13 llg n,m,T,cnt,prime[maxn],mobius[maxn],bj[maxn];
    14 
    15 void init_mobius()
    16 {
    17     mobius[1]=1;
    18     for (llg i=2;i<=N;i++)
    19     {
    20         if (!bj[i])
    21         {
    22             prime[++cnt]=i; mobius[i]=-1;
    23         }
    24         for (llg j=1;j<=cnt && prime[j]*i<=N;j++)
    25         {
    26             bj[i*prime[j]]=1;
    27             if (i%prime[j]) mobius[i*prime[j]]=-mobius[i];
    28             else
    29             {
    30                 mobius[i*prime[j]]=0;
    31                 break;
    32             }
    33         }
    34     }
    35 }
    36 
    37 int main()
    38 {
    39     yyj();
    40     cin>>T;
    41     init_mobius();
    42 //    for (llg i=1;i<=10;i++) cout<<i<<"-->"<<mobius[i]<<endl;
    43     T=10;
    44     while (T--)
    45     {
    46         llg ans=0;
    47         scanf("%lld%lld",&n,&m);
    48         for (llg t=1;t<=n;t++) 
    49         {
    50             ans+=floor((n/t))*floor((m/t))*mobius[t];
    51         }
    52         printf("%lld
    ",ans*4+4);
    53     }
    54     return 0;
    55 }

      二,求${sum_{i=1}^{n}sum_{j=1}^{m}[gcd(i,j)=g],nleq 1e7,mleq 1e7}$

       

      令:${nleq m$}$

      原式${=sum _{g=1}^{n}gsum _{i=1}^{frac{n}{g}}sum _{j=1}^{frac{m}{g}}[gcd(i,j)=1]}$

        ${=sum _{g=1}^{n}gsum _{i=1}^{left lfloor frac{n}{g} ight floor}sum _{j=1}^{left lfloor frac{m}{g} ight floor}sum _{t|i,t|j}mu (t)}$

          ${=sum _{g=1}^{n}gsum_{t=1}^{tleq frac{n}{g}}left lfloor frac{n}{tg} ight floorleft lfloor frac{m}{tg} ight floormu (t)}$

      推到这一步可以发现${sum _{g=1}^{n}sum_{t=1}^{tleq frac{n}{g}}}$是一个调和级数,预处理莫比乌斯函数,然后直接枚举g,然后就可以了,复杂度$O(nlogn)$。

      

      考虑继续优化

      

      令${Q=gt}$

      原式${=sum _{Q=1}^{n}left lfloor frac{n}{Q} ight floorleft lfloor frac{m}{Q} ight floorsum _{g|Q}mu(frac{Q}{g})}$   

      ${ecause sum _{g|Q}mu(frac{Q}{g})g=varphi (Q)}$

      ${ herefore }$原式${=sum _{Q=1}^{n}left lfloor frac{n}{Q} ight floorleft lfloor frac{m}{Q} ight floorvarphi (Q)}$

      可以线性筛预处理${varphi (Q)}$函数,然后可以做$O(n)$做。

      

    本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
  • 相关阅读:
    PHP压缩html网页代码 : 清除空格,制表符,注释标记
    CentOS 编译 Nginx 服务
    Fedora 下安装Fcitx输入法
    SVN 脚本
    Linux 在线播放
    Linux命令行下常用svn命令
    linux vi(vim)常用命令汇总
    MySQL修改root密码
    Fedora 查看CHM帮助文档
    Fedora 快捷键
  • 原文地址:https://www.cnblogs.com/Dragon-Light/p/6364438.html
Copyright © 2020-2023  润新知