hdoj2824 The Euler function(欧拉函数)
来源:互联网 发布:大数据全套视频百度云 编辑:程序博客网 时间:2024/04/29 01:01
3 100
3042
3 100
3042欧拉函数介绍:
先介绍一下暴力的欧拉函数:
Eular(m) = m - (1-1/p1) - (1-1/p2) - ... - (1-1/pk) [其中 p1, p2...pk为m的素因子]
1. phi(p) == p-1 因为素数p除了1以外的因子只有p,所以与 p 互素的个数是 p - 1个
2. phi(p^k) == p^k - p^(k-1) == (p-1) * p^(k-1)
证明:
令n == p^k,小于 n 的正整数共有 p^k-1 个,其中与 p 不互素的个数共 p^(k-1)-1 个,它们是 1*p,2*p,3*p ... (p^(k-1)-1)*p
所以phi(p^k) == (p^k-1) - (p^(k-1)-1) == p^k - p^(k-1) == (p-1) * p^(k-1)。
3. 如果i mod p == 0, 那么 phi(i * p) == p * phi(i) (证明略)举个例子:
假设 p = 3,i = 6,p * i = 18 = 2 * 3^2;
phi(3 * 6) == 18*(1-1/2)*(1-1/3) = 6
p * phi(i) = 3 * phi(6) = 3 * 6 * (1-1/2) * (1-1/3) = 6 = phi(i * p) 正确
4. 如果i mod p != 0, 那么 phi(i * p) == phi(i) * (p-1)
证明:
i mod p 不为0且p为质数, 所以i与p互质, 那么根据积性函数的性质 phi(i * p) == phi(i) * phi(p) 其中phi(p) == p-1
所以 phi(i * p) == phi(i) * (p-1).
再举个例子:
假设i = 4, p = 3, i * p = 3 * 4 = 12
phi(12) = 12 * (1-1/2) * (1-1/3) = 4
phi(i) * (p-1) = phi(4) * (3-1) = 4 * (1-1/2) * 2 = 4 = phi(i * p)正确
打表有两种方法,一种是直接从公式入手,另一种是从欧拉函数的定义入手。方法一(从公式入手)
代码如下:
#include<cstdio>#include<algorithm> #define MAXN 3000300int euler[MAXN]; void init(){ euler[1]=1; for(int i=1;i<MAXN;i++) euler[i]=i; for(int i=2;i<MAXN;i++) { if(euler[i]==i) { //printf("%d\n",i); for(int j=i;j<MAXN;j+=i) euler[j]=euler[j]*(i-1)/i; } }}int main(){init();int a,b;while(scanf("%d%d",&a,&b)!=EOF){__int64 ans=0;for(int i=a;i<=b;i++)ans+=euler[i];printf("%I64d\n",ans);}return 0;}方法二:(从性质定义入手)
代码如下:#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define maxn 3000300int flag[maxn];int phi[maxn];int p[maxn];int get_phi(){int i,j;memset(flag,1,sizeof(flag));int l=0;phi[1]=1; for(i=2;i<maxn;i++){if(flag[i]){p[l++]=i;phi[i]=i-1;}for(j=0;j<l;j++){if(i*p[j]>maxn)break;flag[i*p[j]]=0;if(i%p[j]==0){ phi[i*p[j]]=phi[i]*p[j]; break;} else phi[i*p[j]]=phi[i]*(p[j]-1);} } } int main(){get_phi();/*for(int i=2;i<=100;i++)printf("%d\n",phi[i]);*/__int64 a,b;while(scanf("%d%d",&a,&b)!=EOF){__int64 ans=0;for(int i=a;i<=b;i++){ans+=phi[i];}printf("%I64d\n",ans);}return 0;}
- hdoj2824 The Euler function(欧拉函数)
- The Euler function(hdoj2824)(快速求欧拉函数)
- The Euler function(欧拉函数)
- hdu2824 The Euler function (欧拉函数)
- hdu2824 The Euler function 欧拉函数
- hdu2824 The Euler function 欧拉函数
- 2824 The Euler function【欧拉函数】
- The Euler function【欧拉函数】
- The Euler function(欧拉函数筛选)(转载)
- hdu 2824 The Euler function(欧拉函数)
- hdu2824 The Euler function(欧拉函数个数)
- HDU-#2824 The Euler function(欧拉函数+筛法)
- HDOJ 题目2824 The Euler function(欧拉函数)
- The Euler function(hdu2824,欧拉函数)
- HDU 2824 The Euler function(欧拉函数)
- The Euler function(hdoj --2824-欧拉函数)
- HDU 2824 The Euler function(欧拉函数)
- HDU The Euler function (欧拉函数打表)
- #学志#项目进度01
- CSVファイルを読み込む
- 功能测试用例基础设计模型
- Android4.4蓝牙耳机HFP流程分析-3
- 关于mybatis sql语句的错误笔记
- hdoj2824 The Euler function(欧拉函数)
- JavaSE 学习参考:类的封装
- 初试Kotlin(一)
- GYM 100090 F.Asperger Syndrome(set)
- Importing theano: AttributeError: 'module' object has no attribute 'find_graphviz'
- 运输宝物
- Bluetooth in Android 4.2 and 4.3(一):综述
- mysql 主从数据库同步
- log4j-1.x之三【普通web项目中的高级应用MDC】