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. adb pull 与 push

    adb pull <remote> <local> Copies a specified file from an emulator/device instance to yo ...

  2. RedHat 安装Hadoop并运行wordcount例子

    1.安装 Red Hat 环境 2.安装JDK 3.下载hadoop2.8.0 http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/had ...

  3. BusyBox下ftpget的使用方法

    在终端输入ftpget命令,可以得到以下帮助信息: BusyBox v1.17.4 (2010-12-22 10:59:18 CST) multi-call binary. Usage: ftpget ...

  4. G.易彰彪的一张表

    易彰彪最近有点奇怪,一向爱打游戏他最近居然盯着一张全是大小写字母的表在看,好像在找什么东西.他说,这是他女神给他的一张表,他需要回答女神的问题——在忽略大小写(即大写字母和小写字母视为同一字母)的情况 ...

  5. sed替换变量

    今天在写脚本时用到了sed,我用sed替换xml文件中的变量.一般在sed 中替换都用单引号,如下边 sed -i ‘s/10/1000/g’ test.xml但是如果需要把1000改成变量,如sed ...

  6. Tomcat版本是32位、64位问题

    最近遇到一个Tomcat windows安装版本是32位还是64位问题.由于一系列原因,已经无从知晓生产系统上的该程序是32位还是64位. 后来经过仔细查阅资料,得知: 1. tomcat 从6.0. ...

  7. nyoj 999

    nyoj 999: 点击打开题目链接 题目思路,处理一下地图,把 D E 能看到的地方标记一下.然后就是暴力广搜一下.标记状态,因为同样在同一个点,但是你刚出发到达那点和找到D之后到达相同的点和找到E ...

  8. BZOJ_1295_[SCOI2009]最长距离_dij

    BZOJ_1295_[SCOI2009]最长距离_dij Description windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果从格子A可以走到格子B,那 ...

  9. maven 简单入门教学实战手册

    Maven那点事儿(Eclipse版)   前言: 由于最近工作学习,总是能碰到Maven的源码.虽然平时工作并不使用Maven,但是为了学习一些源码,还是必须要了解下.这篇文章不是一个全面的Mave ...

  10. Flink架构及其工作原理

    目录 System Architecture Data Transfer in Flink Event Time Processing State Management Checkpoints, Sa ...