题意:求∑gcd(i,n),1<=i<=n
思路:
f(n)=∑gcd(i,n),1<=i<=n
可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子。
为什么呢?原因如下:
1到n中有m个数字和n拥有公共的最大因子p,那么就需要把m*p加入答案中。问题是如何计算m的个数。
因为假设某个数i与n的最大公约数为p,那么gcd(i,n) = p,可以得到gcd(i/p,n/p)=1。也就是说,有多少个i,就有多少个i/p与n/p互质。
那么显然m即为n/p的欧拉函数φ(n/p)。

知道了上述之后,其实我们就可以枚举n的因子p(1<=p<=n),若p|n,那么答案加上p*φ(n/p)。

不过《数论及应用》p182上面的解法还利用了积性函数。
积性函数是指一个定义域为正整数n 的算术函数f(n),有如下性质:f(1) = 1,且当a 和b 互质时,f(ab) = f(a) f(b)。
若一个函数f(n) 有如下性质:f(1) = 1,且对两个随意正整数a 和b 而言,不只限这两数互质时,
f(ab) = f(a)f(b) 都成立,则称此函数为完全积性函数。

具体解法可以参见该网址:
http://scturtle.is-programmer.com/posts/19388.html

关于f(N)=∑gcd(i, N)是积性函数的证明参加下面网址:
http://hi.baidu.com/bfcdygoporbjuxr/item/f119741c5fcd9c48e75e06e0

可以推出:f(p^r)=r*(p^r-p^(r-1))+p^r (可以根据φ(p^i)=p^i-p^(i-1)推出)
然后的做法就是将n分解素因子f(n)=f(a1^k1)*f(a2^k2)*...*f(am^km),利用上述公式求解即可。

我的代码就是参照《数论及应用》p182的解法的。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector> using namespace std;
const int maxn=;
long long N;
bool isprime[maxn];
int prime[maxn];
int cnt=;
void init(){
memset(isprime,true,sizeof(isprime));
for(int i=;i<maxn;i++){
if(isprime[i]){
prime[cnt++]=i;
for(int j=*i;j<maxn;j+=i)
isprime[j]=false;
}
}
}
int main()
{
init();
while(scanf("%lld",&N)!=EOF){
long long ans=;
int r;
for(int i=;i<cnt;i++){
if(N%prime[i]==){
long long ret=;
r=;
while(N%prime[i]==){
N=N/prime[i];
ret*=prime[i];
r++;
}
ans*=r*(ret-ret/prime[i])+ret;
}
}
if(N>){
ans*=N-+N;
}
printf("%I64d\n",ans);
}
return ;
}

POJ 2480 Longge's problem (积性函数,欧拉函数)的更多相关文章

  1. [poj 2480] Longge's problem 解题报告 (欧拉函数)

    题目链接:http://poj.org/problem?id=2480 题目大意: 题解: 我一直很欣赏数学题完美的复杂度 #include<cstring> #include<al ...

  2. 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 ...

  3. POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】

    题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...

  4. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

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

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

  6. poj 2480 Longge's problem

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

  7. POJ 2773 Happy 2006------欧几里得 or 欧拉函数。

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8359   Accepted: 2737 Descri ...

  8. HDU 6322.Problem D. Euler Function -欧拉函数水题(假的数论题 ̄▽ ̄) (2018 Multi-University Training Contest 3 1004)

    6322.Problem D. Euler Function 题意就是找欧拉函数为合数的第n个数是什么. 欧拉函数从1到50打个表,发现规律,然后勇敢的水一下就过了. 官方题解: 代码: //1004 ...

  9. 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)

    题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...

随机推荐

  1. 浅谈HAL

    参考:http://blog.csdn.net/mr_raptor/article/details/8074549 代码实现:http://blog.csdn.net/mr_raptor/articl ...

  2. JavaScript高级程序设计之元素大小

    1.偏移量 // 元素相对于文档的偏移量 var getOffSet = function (ele) { var actualLeft = ele.offsetLeft, // 相对于offsetP ...

  3. 在WIN7下安装运行mongodb 1)、下载MongoDB

    1).下载MongoDB http://downloads.mongodb.org/win32/mongodb-win32-i386-2.4.5.zip 下载Windows 32-bit版本并解压缩, ...

  4. jquery介绍

    1.jQuery (1)jQuery简介 是一个js框架(.js文件),它的最大特点是,使用选择器( 借鉴了css选择器的语法)查找要操作的节点,并且将这些 节点封装成一个jQuery对象,通过调用j ...

  5. 通过修改注册表来破解sqlyog

    Sqlyog作为一款可视化的数据库管理工具,各种方便我就不说了,但是未经汉化或者绿色过的软件存在30天的生命期,到期后我们就不可以使用了,要摸卸载重装,我们还可以去修改注册表,来延长它的生命期,具体步 ...

  6. 【吐血推荐】简要分析unity3d中剪不断理还乱的yield

    在学习unity3d的时候很容易看到下面这个例子: void Start () { StartCoroutine(Destroy()); } IEnumerator Destroy(){ yield ...

  7. 新装Centos常见问题及解决方案

    1.可以ping通,但无法通过ssh连接虚拟机的解决方案 虚拟机上装了一个 Linux 玩玩, 但在启动 Linux 后,在 Windows 中通过 Xshell 以 SSH 方式连接到 Linux ...

  8. 学习Linux第五天

    1.VIM编辑器 3种模式: Command Model , Insert Model , Last line Model 安装vim: sudo apt-get install vim 如果提示出错 ...

  9. Ming Rpc

    原文地址:http://iwantmoon.com/Post/487ab43d609f49d28ff4228241e2b7c7 Rpc(Remote Procedure Call Protocal)远 ...

  10. C++ Template之非类型模板参数

    非类型模板参数是通过基本变量类型引入,例如int,在使用时必须显式自定值,不能通过推断. 非类型模板参数的限制:不能是浮点数(在vc6.0上测试可以为浮点型),对象以及指向内部链接对象的指针. #in ...