HYSBZ - 2818 Gcd (莫比乌斯反演)

莫比乌斯反演的入门题,设 \(F(x): gcd(i,j)\%x=0\) 的对数,\(f(x): gcd(i,j)=x\)的对数。
易知$$F(p) = \lfloor \frac{n}{p} \rfloor * \lfloor \frac{n}{p} \rfloor$$
\(F(x) = \sum_{x|d} f(d)\)
根据莫比乌斯反演得,\(f(x) = \sum_{x|d}u(\frac{d}{x})F(d)\)
所求的是\(gcd(i,j)\)为素数的对数,所以\(ans = \sum_{isprime(p)}f(p)\)

\[ans = \sum_{p}^{N}\lfloor \frac{N}{d}\rfloor*\lfloor \frac{N}{d}\rfloor\sum_{p|d}u(\frac{d}{p}) \]

其中\(d=i*p\)
\(\sum_{p|d}u(\frac{d}{p})\)打表求出

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e7+5;
bool vis[maxn];
int prime[maxn],mu[maxn];
int sum[maxn];
void init(){
    memset(vis,false,sizeof(vis));
    mu[1] = 1;
    prime[0] = 0;
    int cnt=0;
    for(int i=2;i<maxn;++i){
        if(!vis[i]){
            mu[i] = -1;
            sum[i] = 1;
            prime[++cnt] = i;
        }
        for(int j=1;j<=cnt;++j){
            if(i*prime[j] >= maxn)  break;
            vis[i*prime[j]] = true;
            if(i % prime[j]){
                mu[i*prime[j]] = -mu[i];
                sum[i*prime[j]] = mu[i] - sum[i];
            }
            else{
                mu[i*prime[j]] = 0;
                sum[i*prime[j]] = mu[i];
                break;
            }
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    init();
    LL N;
    while(scanf("%lld",&N)==1){
        LL res=0;
        for(LL i=1;i<=N;++i){
            res += sum[i]*(N/i)*(N/i);
        }
        printf("%lld\n",res);
    } 
    return 0;
}

posted @ 2018-08-25 19:23  xiuwenL  阅读(328)  评论(0编辑  收藏  举报