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. Ajax的简单实现(Json)

    之前写的是一般的Ajax if (request.status === 200) { document.getElementById("createResult").innerHT ...

  2. activity栈管理的3种方式

    一.背景 在android开发过程最经常使用的组件非activity莫属. 通过分析activity的各种跳转,执行同学能够分析用户的各种行为.更重要的一点是在做插件化的过程中,我们经常会对activ ...

  3. 网页 H5“线条” 特效实现方式(canvas-nest)

    先上图 (看博客空白处也可以呦): 前一阵浏览网站的时候,发现了这个好玩的东西,一直想找找怎么实现的,今天忙里偷闲,上网搜了一下,发现实现起来特别简单. 只需要在网页body里引入一个<scri ...

  4. SharePoint 2010 Pop-Up Dialogs SharePoint 2010 弹出对话框

    SharePoint 2010 Pop-Up Dialogs SharePoint 2010 弹出对话框         SharePoint 2010 使得往你的站点加入对话框内容变得出乎意料的简单 ...

  5. Linux搭建lnmp环境

    在CentOS 6上使用yum安装lnmp服务,原文链接http://www.qiansw.com/yum-lnmp.html

  6. linux地址映射1、2、3(⭐⭐⭐)

    欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 一.线性映射与非线性映射                                                   ...

  7. seafile看不见repo报500错误的解决方法

    环境 seafile-server-6.2.5 centos7.5 1804 现象 seafile服务器所在的VPS没动过,前一天seafile用还好好的,昨天客户端突然不能登录了,显示“服务器内部错 ...

  8. Tomcat调优策略

    Jmeter压力测试工具 JMeter是一款在国外非常流行和受欢迎的开源性能测试工具,像LoadRunner 一样,它也提供了一个利用本地Proxy Server(代理服务器)来录制生成测试脚本的功能 ...

  9. lucene Index Store TermVector 说明

    最新的lucene 3.0的field是这样的: Field options for indexingIndex.ANALYZED – use the analyzer to break the Fi ...

  10. 如何在cowboy应用中指定mnesia数据库路径

    创建mnesia数据库的步骤简述: 1)定义脚本: -module(mns). -export([setup/0, clean/0]). -record(user, { id, coin, diamo ...