BZOJ2705: [SDOI2012]Longge的問題

Description
Longge的數學成績非常好,並且他非常樂於挑戰高難度的數學問題。現在問題來了:給定一個整數N,你需要求出∑gcd(i, N)(1<=i
<=N)。
Input
一個整數,爲N。
Output
一個整數,爲所求的答案。
Sample Input
6
Sample Output
15
HINT
【數據範圍】
對於60%的數據,0<N<=216
對於100%的數據,0<N<=232
Source
round1 day1

枚舉n 的約數k ,設s(k) 爲滿足gcd(m,n)=k(1mn) 的個數,則ans=(ks(k)) 。因爲gcd(m,n)=k ,所以gcd(m/k,n/k)=1 ,於是s(k)=φ(n/k)

#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
typedef long long ll;
ll n,ans;
void read(ll &x)
{
    char t=getchar();int f=1;x=0;
    while((t<48)or(t>57)){if(t=='-')f=-1;t=getchar();}
    while((t>=48)and(t<=57)){x=(x*10)+t-48;t=getchar();}
    x*=f;
}
ll phi(ll x)
{
    ll ans=1;
    for (int i=2;i*i<=x;++i)
    if (x%i==0)
    {
        ans*=(i-1);
        x/=i;
        while (x%i==0)
        {
            ans*=i;
            x/=i;
        }
    }
    if (x>1) ans*=(x-1);
    return ans;
}
int main()
{
    read(n);
    for (int i=1;i*i<=n;++i)
    if (n%i==0)
    {
        ans+=i*phi(n/i);
        if (i*i!=n) ans+=(n/i)*phi(i);//對應的另一約數
    }
    cout<<ans;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章