• bzoj2705: [SDOI2012]Longge的问题


    2705: [SDOI2012]Longge的问题

    Time Limit: 3 Sec  Memory Limit: 128 MB

    Description

    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。

     

    Source

    round1 day1

    Tip:

      按gcd的不同,题目可以转化为:sigma(g*与n的最大公约数为g的数的个数)
      先考虑与n的gcd=1的i,它的数量为 φ(n)
      假设 gcd(i, N)=g,i=g*a,n=g*b,那么i与n同除以g后互质,也就是说:与n的最大公约数为g的数的个数为 φ(n/g)

      可以在sqrt(n)范围内枚举n的因子,并求出其欧拉函数值

    Code:

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    long long n,ans;
    
    long long phi(long long x){
        long long res=x;
        long long y=x;
        for(int i=2;i*i<=y;i++)
            if(x%i==0){
                res=res/i*(i-1);
                while(x%i==0) x=x/i;
            }
        if(x>1) res=res/x*(x-1);
        return res;
    }
    
    int main(){
        scanf("%lld",&n);
        for(long long i=1;i*i<=n;i++)
            if(n%i==0){
                ans+=i*phi(n/i);
                if(i!=n/i) ans+=(n/i)*phi(i);
            }
        printf("%lld",ans);
    }
  • 相关阅读:
    hdoj 1002 A + B Problem II
    hdoj 1234 开门人和关门人
    hdoj 2203 亲和串
    nyoj 73 比大小
    81B
    信息传递
    bzoj1787
    最少交换次数
    100803C
    火柴排队
  • 原文地址:https://www.cnblogs.com/WQHui/p/8421894.html
Copyright © 2020-2023  润新知