BZOJ2705[Longge的問題] 歐拉函數

Description

Longge的數學成績非常好,並且他非常樂於挑戰高難度的數學問題。現在問題來了:給定一個整數N,你需要求出∑gcd(i, N)(1<=i <=N)。

Input

一個整數,爲N。

Output

一個整數,爲所求的答案。

Sample Input

6

Sample Output

15


解題報告:

令 k|n,m|n gcd(m,n)=k , 令s(k)爲gcd爲k滿足條件的m的個數。

gcd(m/k,n/k)=1, s(k)=euler(n/k)

ans=∑k*φ(n/k) (k|n)


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define LL long long
LL n, ans;
LL phi( LL x ){
    LL m=sqrt(x+0.5);
    LL ans = x;
    for ( LL i=2; i<=m; i++) if( x%i==0 ){
        ans = ans/i*(i-1);
        while( x%i==0 ) x/=i;
    }
    if( x>1 ) ans = ans/x*(x-1);
    return ans;
}

int main(){
    scanf("%lld", &n );
    ans = 0;
    for ( LL i=1; i*i<=n; i++ )
        if( n%i==0 ){
            ans+=(LL)i*phi(n/i);
            ans+=(LL)(n/i)*phi(i);
        }
    printf("%lld", ans);
    return 0;
}
發佈了190 篇原創文章 · 獲贊 19 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章