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. 
"Oh, I know, I know!" Longge shouts! But do you know? Please solve it. 

Input

Input contain several test case. 
A number N per line. 

Output

For each N, output ,∑gcd(i, N) 1<=i <=N, a line

Sample Input

2
6

Sample Output

3
15
解题思路:给出一个数n,求1-n这n个数与n的最大公约数之和。举个栗子:当n=4时,1,2,3,4与4的最大公约数分别为1,2,1,4,累加和为8。正解:1-n中每个数与n的最大公约数肯定是n的一个因子,所以我们只需要枚举n的每一个因子x∈[1,√n],然后看有多少个满足gcd(k,n)==x,即求满足gcd(k/x,n/x)==1中k的个数(用欧拉函数求解),则公式为:∑x*[gcd(k/x,n/x)==1]。
AC代码(204ms):
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 1e6+;
LL n, ans;
LL get_Euler(LL x){
LL res = x;
for(LL i = 2LL; i * i <= x; ++i) {
if(x % i == ) {
res = res / i * (i - );
while(x % i == ) x /= i;
}
}
if(x > 1LL) res = res / x * (x - );
return res;
} int main(){
while(cin >> n) {
ans = 0LL;
for (LL i = 1LL; i * i <= n; ++i) {
if(n % i == ) {
ans += i * get_Euler(n / i);
if(i * i != n) ans += n / i * get_Euler(i); ///避免重复计数
}
}
cout << ans << endl;
}
return ;
}
AC代码二(32ms):思路和上面相同,只是将问题求解转换一下gcd(i, n) == (p_i)^j,即求Σ(p_i)^j [gcd(i/((p_i)^j)), n/((p_i)^j)==1],化简公式得 (k+1)* p^k - k*p^(k-1),再根据积性函数的性质得n的欧拉函数值为每种素因子对应的欧拉函数值φ((p_i)^a_i)相乘即可。时间复杂度是O(sqrt(n))。具体推导过程:传送门
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL n;
LL solve(LL x) {
LL p_i, k, ans = 1LL;
for(LL i = 2LL; i * i <= x; ++i) {
if(x % i == ) {
p_i = 1LL, k = ;
while(x % i == ) {k++, p_i *= i, x /= i;}
ans *= (k + ) * p_i - k * p_i / i; ///(k+1)*p^k - k*p^(k-1)
}
}
if(x > 1LL) ans *= * x - 1LL;
return ans;
}
int main() {
while(cin >> n) {
cout << solve(n) << endl;
}
return ;
}

题解报告:poj 2480 Longge's problem(欧拉函数)的更多相关文章

  1. poj 2480 Longge's problem [ 欧拉函数 ]

    传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2 ...

  2. POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6383   Accepted: 2043 ...

  3. poj 2480 Longge's problem 欧拉函数+素数打表

    Longge's problem   Description Longge is good at mathematics and he likes to think about hard mathem ...

  4. 题解报告:hdu 2588 GCD(欧拉函数)

    Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written ...

  5. poj 2480 Longge's problem 积性函数

    思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e ...

  6. POJ 2480 Longge's problem (积性函数,欧拉函数)

    题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...

  7. poj 2480 Longge's problem

    /** 大意: 计算f(n) = ∑ gcd(i, N) 1<=i <=N. 思路: gcd(i,x*y) = gcd(i,x) * gcd(i, y ) 所以gcd 为积性函数 又因为积 ...

  8. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  9. Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1959  Solved: 1229[Submit][ ...

随机推荐

  1. HUNNU-10307-最优分解问题

    点击打开题目连接 # include <queue> # include <cstdio> # include <cstring> # include <io ...

  2. gdb调试使用autotools工程的项目

    1 保留debug sympol和优化等级设置为-O0的最简单的方法 在执行configure脚本生成Makefile文件时,使用CXXFLAGS宏,因为标准的configure脚本给了这个宏. .. ...

  3. How do I set the timeout for a JAX-WS webservice client?

    How do I set the timeout for a JAX-WS webservice client? up vote58down votefavorite 27 I've used JAX ...

  4. (最新)各大公司Java后端开发面试题总结

    ThreadLocal(线程变量副本) Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量. 采用空间换时间,它用于线程间的数据隔离,为每一个使用该变量的线程提供一 ...

  5. 基于Ubuntu 14.04 LTS编译Android4.4.2源码

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS ...

  6. ios 关于动画用法的总结

      #import "FirstVC.h" @implementation FirstVC /*     创建xib过程     1 创建xib(名字和类名相同)     2 文件 ...

  7. Linux内核中kzalloc函数详解

    **************************************************************************************************** ...

  8. VC++配置OpenGL开发环境

    目录 第1章配置    1 第2章核心文件    6 2.1 核心文件    6 2.2 编译时使用核心文件    6 2.3 运行时使用核心文件    7 2.4 依赖关系    7 第3章 AUX ...

  9. jsch上传文件功能

    转载:http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html JSch是Java Secure Channel的缩写.JSch是一个 ...

  10. Intelidea右键新建选项没有Java class选项

    Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure