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

    传送门

    题目描述很简洁,求(sum_{i=1}^ngcd(i,n))

    由于我们难以直接求出(gcd),所以我们换一种比较套路的做法:枚举(gcd),转化为数论函数计算。

    根据欧拉函数的性质:(n = sum_{d|n}varphi(d)),那么我们就能把式子改写一下,得到

    [sum_{i=1}^nsum_{d|i,d|n} varphi(d) ]

    转换一下可以得到:

    [sum_{i=1,d|i}^nsum_{d|n} varphi(d) ]

    前面那一项就是(frac{n}{d}) ,把两项调换一下

    所以结果就是 $$sum_{d|n}d varphi(frac{n}{d})$$

    或者我们可以换一种推导方法,同样是枚举gcd,这次我们改写成这个形式:

    [sum_{d|n}sum_{i=1}^n[gcd(i,n) == d] ]

    把d除进去,就有:

    [sum_{d|n}dsum_{i=1}^{frac{n}{d}}[gcd(i,frac{n}{d}) == 1] ]

    然后后面其实就是一个欧拉函数的形式,所以结果就是:

    [sum_{d|n} d varphi(frac{n}{d}) ]

    我们只要每次在(sqrt{n})的范围内枚举质因子,然后在(O(sqrt{n}))的时间内计算欧拉函数即可。注意要开(longlong)

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<set>
    #include<vector>
    #include<map>
    #include<queue>
    #define rep(i,a,n) for(int i = a;i <= n;i++)
    #define per(i,n,a) for(int i = n;i >= a;i--)
    #define enter putchar('
    ')
    #define fr friend inline
    #define y1 poj
    #define mp make_pair
    #define pr pair<int,int>
    #define fi first
    #define sc second
    #define pb push_back
    #define I puts("bug")
    
    using namespace std;
    typedef long long ll;
    const int M = 200005;
    const int INF = 1000000009;
    const double eps = 1e-7;
    const double pi = acos(-1);
    const ll mod = 1e9+7;
    
    ll read()
    {
        ll ans = 0,op = 1;char ch = getchar();
        while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
        while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
        return ans * op;
    }
    
    ll n,p[M],tot;
    bool np[M];
    
    ll phi(ll x)
    {
       ll ans = x,m = sqrt(x);
       rep(i,2,m) if(!(x % i)) {ans = ans - ans / i; while(!(x%i)) x /= i;}
       if(x > 1) ans = ans - ans / x;
       return ans;
    }
    
    ll calc(ll x)
    {
       ll cur = 0;
       for(int i = 1;(ll)i * i < x;i++) if(x % i == 0) cur += (ll)i * phi(x/i) + (ll)(x/i) * phi(i);
       if((ll)sqrt(x) * sqrt(x) == x) cur += (ll)sqrt(x) * phi(sqrt(x));
       return cur;
    }
    
    int main()
    {
       n = read();
       printf("%lld
    ",calc(n));
       return 0;
    }
    
    
  • 相关阅读:
    Linux下文件属性(drwxr-xr-x)详解以及(-rwxrwxrwx=777)(转)
    Linux中chown和chmod的区别和用法(转)
    以root用户身份在jenkins中运行shell命令
    Java中Properties配置文件读取
    Java分布式服务框架Dubbo初探(待实践)
    Servlet3.0新特性
    Java中System.getProperty()的参数
    Java获取路径的方法分析详解(Application/Web)
    Java中的JAR/EAR/WAR包的文件夹结构说明(转)
    JAR包中的MANIFEST.MF文件详解以及编写规范
  • 原文地址:https://www.cnblogs.com/captain1/p/10113690.html
Copyright © 2011-2022 走看看