poj2480 Longge's problem

欲求 \(\sum_{i=1}^n (i,n)\)。

显然 \((i,n) \mid n\)。记 \(d=(i,n)\),枚举 \(d\),有多少个 \(i \in [1,n]\) 使得 \((i,n)=d\) 呢?换句话说有多少个 \(i \in [1,\lfloor n/d \rfloor]\) 使得 \((i,\lfloor n/d \rfloor) = 1\) 呢?显然是 \(\varphi(\lfloor n/d \rfloor)\) 个。

答案就是 \(\sum_{d|n}d\cdot \varphi(n/d)\)。

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n, T;
ll ans;
ll phi(ll x){
    ll re=x;
    for(ll i=2; i*i<=x; i++)
        if(x%i==0){
            re = re / i * (i - 1);
            while(x%i==0)   x /= i;
        }
    if(x>1) re = re / x * (x - 1);
    return re;
}
int main(){
    while(scanf("%d", &n)!=EOF){
        ans = 0;
        for(ll i=1; i*i<=n; i++)
            if(n%i==0){
                ans += (ll)i * phi(n/i);
                if(i*i!=n)  ans += (ll)(n/i) * phi(i);
            }
        printf("%lld\n", ans);
    }
    return 0;
}

poj2480 Longge's problem

原文地址:https://www.cnblogs.com/poorpool/p/8550168.html

时间: 2024-11-14 13:08:50

poj2480 Longge's problem的相关文章

POJ2480 Longge&#39;s problem 欧拉函数的应用 &amp;&amp; 积性函数

题意很简单,求sum(gcd(i,n))   1<=i<=n; 这题看到后第一反应并没有里用积性函数的性质,不过也可以做,欣慰的是我反应还是比较快的 设f(n)=gcd(1,n)+gcd(2,n)+....+gcd(n-1,n) + gcd(n,n), 用g(n,i)表示满足 gcd(x,n)=i的 x的个数 (x小于n),则 f(n)=sum{i*g(n,i)}; 同时又利用 扩展欧几里德的性质  gcd(x,n)=i  的充要条件是 gcd(x/i,n/i)==1,所以 满足 x/i的解有

POJ2480:Longge&#39;s problem(欧拉函数的应用)

题目链接:传送门 题目需求: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. 这题就是上一篇博客的变形. 题目解析:首先先求出与N互质的个数,即N的欧拉函数值,之后分解出N的因子来,求解方法如下. 证明: 要求有多少个 i 满足gcd(i, N) = d 如果gcd(i, N) = d,则gcd(i/d, N/d) = 1 由于i <= N,所以 i/d <= N/d,

Longge&#39;s problem ( gcd的积性)

Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. "Oh, I

【POJ 2480】Longge&#39;s problem(欧拉函数)

题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 分析 用欧拉函数$φ(x)$求1到x-1有几个和x互质的数. gcd(i,n)必定是n的一个约数.若p是n的约数,那么gcd(i,n)==p的有$φ(n/p)$个数,因为要使gcd(i,n)==p,i/p和n/p必须是互质的.那么就是求i/p和n/p互质的i在[1,n]里有几个,就等价于,1/p,2/p,...,n/p里面有几个和n/p互质,即φ(n/p). 求和的话,约数为p

POJ 2480 Longge&#39;s problem 积性函数

题目来源:POJ 2480 Longge's problem 题意:求i从1到n的gcd(n, i)的和 思路:首先如果m, n 互质 gcd(i, n*m) = gcd(i, n)*gcd(i, m) 这是一个积性函数积性函数的和还是积性函数 由欧拉函数知识得 phi(p^a) = p^a - p^(a-1) p是素数 a是正整数 得到最终答案f(n) = f(p1^a1)*f(p2^a2)*...*f(pn^an) 其中f(p^a) = a*(p^a-p^(a-1))+p^a #includ

poj 2480 Longge&#39;s problem 积性函数性质+欧拉函数

题意: 求f(n)=∑gcd(i, N) 1<=i <=N. 分析: f(n)是积性的数论上有证明(f(n)=sigma{1<=i<=N} gcd(i,N) = sigma{d | n}phi(n / d) * d ,后者是积性函数),可以这么解释:当d是n的因子时,设1至n内有a1,a2,..ak满足gcd(n,ai)==d,那么d这个因子贡献是d*k,接下来证明k=phi(n/d):设gcd(x,n)==d,那么gcd(x/d,n/d)==1,所以满足条件的x/d数目为phi(

poj 2480 Longge&#39;s problem [ 欧拉函数 ]

传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2416 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms.

POJ 2480 Longge&#39;s problem (欧拉函数+乘性函数)

Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7343   Accepted: 2422 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now

Longge&#39;s problem(欧拉函数应用)

Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N.