• P2303 [SDOI2012]Longge的问题 我傻QwQ


    莫比乌斯反演学傻了$QwQ$


    思路:推式子?

    提交:2次

    错因:又双叒叕没开$longspace long$ 

    题解:

    $sum_{i=1}^n gcd(i,n)$

    $=sum_{d|n}dsum_{i=1}^{frac{n}{d}} [gcd(i,frac{n}{d})=1]$

    注意到$sum_{i=1}^{frac{n}{d}} [gcd(i,frac{n}{d})=1]$就是与$frac{n}{d}$互质的数的个数。

    $=sum_{d|n}dvarphi(frac{n}{d})$

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    using namespace std;
    #define ull unsigned long long
    #define ll long long
    #define R register ll
    #define pause (for(R i=1;i<=10000000000;++i))
    #define In freopen("NOIPAK++.in","r",stdin)
    #define Out freopen("out.out","w",stdout)
    namespace Fread {
    static char B[1<<15],*S=B,*D=B;
    #ifndef JACK
    #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
    #endif
    inline ll g() {
        R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
        if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
    } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);}
    inline void gs(char* s) {
        register char ch; while(isempty(ch=getchar()));
        do *s++=ch; while(!isempty(ch=getchar()));
    }
    } using Fread::g; using Fread::gs;
    namespace Luitaryi {
    const int N=1<<16+10;
    int n,cnt; ll ans;
    int pri[N>>3]; bool vis[N];
    inline void PRE(int n) { 
        for(R i=2;i<=n;++i) {
            if(!vis[i]) pri[++cnt]=i;
            for(R j=1;j<=cnt&&i*pri[j]<=n;++j) {
                vis[i*pri[j]]=true; if(i%pri[j]==0) break;
            }
        }
    }
    inline ll phi(ll x) { R ret=x;
        for(R i=1;i<=cnt&&1ll*pri[i]*pri[i]<=x;++i) if(x%pri[i]==0) {
            ret=ret/pri[i]*(pri[i]-1); while(x%pri[i]==0) x/=pri[i];
        } if(x>1) ret=ret/x*(x-1); return ret;
    }
    inline void main() {
        n=g(); R lim=sqrt(n); PRE(lim);
        for(R i=1;i<=lim;++i) if(n%i==0) {
            ans+=i*phi(n/i);
            if(i*i!=n) ans+=n/i*phi(i);
        } printf("%lld
    ",ans);
    }
    }
    signed main() {
        Luitaryi::main();
    }

    2019.07.18

  • 相关阅读:
    如何将本地代码上传到GitHub
    《剑指offer》JavaScript版19-21题
    《剑指offer》JavaScript版16-18题
    《剑指offer》JavaScript版13-15题
    蘑菇街(前端1面)
    《二十二》观后感
    前端面试题(2)
    《深入理解ES6》之Promise
    HDU 4939 Stupid Tower Defense (2014 Multi-University Training Contest 7)
    HDU 4940 Destroy Transportation system(2014 Multi-University Training Contest 7)
  • 原文地址:https://www.cnblogs.com/Jackpei/p/11211119.html
Copyright © 2020-2023  润新知