zoukankan      html  css  js  c++  java
  • HYSBZ

    莫比乌斯反演的入门题,设 (F(x): gcd(i,j)\%x=0) 的对数,(f(x): gcd(i,j)=x)的对数。
    易知$$F(p) = lfloor frac{n}{p} floor * lfloor frac{n}{p} floor$$
    (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} floor*lfloor frac{N}{d} floorsum_{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
    ",res);
        } 
        return 0;
    }
    
    
    为了更好的明天
  • 相关阅读:
    python全栈闯关--16-匿名函数
    python全栈闯关--15-内置函数
    python全栈闯关--14-生成器进阶
    示例库
    MySQL的远程连接
    前后端传输编码方式
    后端接收前端时间参数
    控制器接参的空值问题
    MyBatis模糊查询的几种方式
    MySQL常用函数
  • 原文地址:https://www.cnblogs.com/xiuwenli/p/9535065.html
Copyright © 2011-2022 走看看