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的因子。
为什么呢?原因如下:
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 (积性函数,欧拉函数)的更多相关文章
- [poj 2480] Longge's problem 解题报告 (欧拉函数)
题目链接:http://poj.org/problem?id=2480 题目大意: 题解: 我一直很欣赏数学题完美的复杂度 #include<cstring> #include<al ...
- 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 ...
- POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】
题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...
- 题解报告:poj 2480 Longge's problem(欧拉函数)
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 2 ...
- poj 2480 Longge's problem
/** 大意: 计算f(n) = ∑ gcd(i, N) 1<=i <=N. 思路: gcd(i,x*y) = gcd(i,x) * gcd(i, y ) 所以gcd 为积性函数 又因为积 ...
- POJ 2773 Happy 2006------欧几里得 or 欧拉函数。
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8359 Accepted: 2737 Descri ...
- HDU 6322.Problem D. Euler Function -欧拉函数水题(假的数论题 ̄▽ ̄) (2018 Multi-University Training Contest 3 1004)
6322.Problem D. Euler Function 题意就是找欧拉函数为合数的第n个数是什么. 欧拉函数从1到50打个表,发现规律,然后勇敢的水一下就过了. 官方题解: 代码: //1004 ...
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
随机推荐
- C/C++走过的坑(基础问题篇)
1.有符号int与无符号int比较 #define TOTOL_ELEMENTS (sizeof(a) / sizeof(a[0]) ); int main() { int a[] = {23,24, ...
- 263. Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- EMVTag系列3《持卡人基本信息数据》
Ø 9F61 持卡人证件号 L:2–26 R(需求):数据应存在,在读应用数据过程中,终端不检查: (PBOC2.0第五部分中规定)芯片中持卡人姓名 5F20与持卡人姓名扩展9F0B只能使用一 ...
- ED/EP系列4《圈存指令》
1. 圈存交易 通过圈存交易,持卡人可将其在银行相应账户上的资金划入电子存折或电子钱包中. 特点: 1)--必须在金融终端上联机进行; 2)--必须提交个人识别码(PIN) 步骤: 1) --终端: ...
- Java入门到精通——工具篇之Maven概述
为接手gxpt准备已经快一个月了从SSH2-->EJB-->环境搭建-->Maven的构建.下面就带领大家初始Maven 一.什么是Maven. Maven是一个垮平台的项目管理工具 ...
- 设置SVN hooks实现自动发布
# CentOS下安装SVN yum -y install subversion svnserve --version cd /home/ mkdir svn cd svn/ /usr/bin/svn ...
- hdu 1509 Windows Message Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...
- wget的下载与安装使用
wget的下载与安装:下载地址:ftp://ftp.cs.cuhk.edu.hk/pub/gnu/gnu/wget安装:先把wget下载到的tar文件解压,然后cd到wget目录下# ./config ...
- JVM学习总结五(番外)——JConsole
之前本来打算结合自己写的小程序来介绍JConsole和VirtualVM的使用的,但是发现很难通过一个程序把所有的场景都体现出来,所以还是决定用书中的典型小例子来讲更加清晰. 一.JConsole的基 ...
- Boolean.parseBoolean("true") 和 Boolean.getBoolean("true");的区别及用法
正确用法:boolean repeatIndicator = Boolean.valueOf("true").booleanValue();或者也可以使用Boolean.parse ...