zoukankan      html  css  js  c++  java
  • [SDOI2012]Longge的问题

    Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题。现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N)。

    Input

    一个整数,为N。

    Output

    一个整数,为所求的答案。

    Sample Input

    6

    Sample Output

    15

    HINT

    【数据范围】

    对于60%的数据,0<N<=2^16。

    对于100%的数据,0<N<=2^32。

    这道题模拟一下样例:
    1: 1,5
    2: 2,4
    3:3
    6:6
    sum=1*2+2*2+3*1+6*1=15
    我们发现2中有的数目就是除以2后和n除以2后互质的数的个数,就是phi(n/2)啦
    同理求个和就行。
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #define ll long long
    using namespace std;
    ll n,ans=0;
    ll phi(ll x)
    {
        if(x==1) return 1;
        ll sqr_=sqrt(x);
        ll tot=x;
        for(ll i=2;i<=sqr_&&x;i++)
        {
            if(x%i==0) tot=tot*(i-1)/i;
            while(x%i==0)
            {
                x/=i;
            }
        }
        if(x>1)
        tot=tot*(x-1)/x;
        return tot; 
     } 
    int main()
    {
        scanf("%lld",&n);
        ll sqr=sqrt(n);
        for(ll i=1;i<=sqr;i++)
        if(n%i==0)
        {
            ans+=i*phi(n/i);
            if(i*i<n)ans+=phi(i)*(n/i);
            //cout<<n/i<<' '<<phi(n/i)<<' '<<ans<<endl;
        } 
        cout<<ans<<endl;
     } 
  • 相关阅读:
    iis环境异常处理
    cmd常用命令:关机、注销、进入d盘等
    position
    Register Form
    第一周
    Django简介
    前端jQuery基本语法
    前端基础BOM和DOM
    HTML常用标签
    Linux相关 MySQL相关
  • 原文地址:https://www.cnblogs.com/dancer16/p/7346290.html
Copyright © 2011-2022 走看看