zoukankan      html  css  js  c++  java
  • P1390 公约数的和

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #define inf 2147483647
    #define N 2000010
    #define mod 1000000007
    #define p(a) putchar(a)
    #define For(i,a,b) for(long long i=a;i<=b;++i)
    //by war
    //2019.8.21
    using namespace std;
    long long n,tot[N],num,cnt,ans,temp;
    long long prime[N],mu[N];
    bool vis[N];
    void in(long long &x){
        long long y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(long long x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void Euler(){
        mu[1]=1;
        For(i,2,2e6){
            if(!vis[i]) prime[++cnt]=i,mu[i]=-1;
            for(long long j=1;j<=cnt&&i*prime[j]<=2e6;j++){
                vis[i*prime[j]]=1;
                if(i%prime[j]==0){
                    mu[i*prime[j]]=0;
                    break;
                }
                mu[i*prime[j]]=-mu[i];
            }
        }
    }
    
    signed main(){
        in(n);
        Euler();
        For(i,1,n){
            temp=0;
            for(long long d=i;d<=n;d+=i){
                num=n/d;
                temp+=mu[d/i]*num*(num-1)/2;
            }
            ans+=temp*i;
        }
        o(ans);
        return 0;
    }
  • 相关阅读:
    POJ 1637:Sightseeing tour
    bzoj 3997: [TJOI2015]组合数学
    [CEOI2008]order
    【网络流24题】星际转移问题
    Codeforces Round #460 D. Karen and Cards
    bzoj 3142: [Hnoi2013]数列
    codeforces586B
    codeforces631B
    codeforces548B
    codeforces515B
  • 原文地址:https://www.cnblogs.com/war1111/p/11391153.html
Copyright © 2011-2022 走看看