The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4518    Accepted Submission(s): 1879

Problem Description
The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very
easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
 
Input
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
 
Output
Output the result of (a)+ (a+1)+....+ (b)
 
Sample Input
3 100
 
Sample Output
3042
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2818 2825 2817 2822 2821

解析:(转)

定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质的数的数目。

    例如:φ(8)=4,因为1,3,5,7均和8互质。

性质:1.若p是质数,φ(p)=
p-1.

   2.若n是质数p的k次幂,φ(n)=(p-1)*p^(k-1)。因为除了p的倍数都与n互质

   3.欧拉函数是积性函数,若m,n互质,φ(mn)=
φ(m)φ(n).

  根据这3条性质我们就可以推出一个整数的欧拉函数的公式。因为一个数总可以写成一些质数的乘积的形式。

  E(k)=(p1-1)(p2-1)...(pi-1)*(p1^(a1-1))(p2^(a2-1))...(pi^(ai-1))

    = k*(p1-1)(p2-1)...(pi-1)/(p1*p2*...*pi)

    = k*(1-1/p1)*(1-1/p2)...(1-1/pk)

在程序中利用欧拉函数如下性质,可以快速求出欧拉函数的值(a为N的质因素)

  若( N%a
==0&&(N/a)%a
==0)则有:E(N)=
E(N/a)*a;

  若( N%a
==0&&(N/a)%a
!=0)则有:E(N)=
E(N/a)*(a-1);

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=3000010;
int prime[N],isprime[N];
int phi[N];
void get_phi()
{
int i,j,cnt=0;
for(i=2;i<N;i++)
{
if(isprime[i]==0)
{
prime[cnt++]=i;
phi[i]=i-1;
}
for(j=0;j<cnt && i*prime[j]<N;j++)
{ //注意这里,i*prime[j]<N 可换成 prime[j]<=N/i(带等号)
isprime[i*prime[j]]=1;
if(i%prime[j]==0)
phi[i*prime[j]]=phi[i]*prime[j];
else
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
int main()
{
long long sum;
int a,b;
get_phi();
while(~scanf("%d%d",&a,&b))
{
sum=0;
for(int i=a;i<=b;i++)
sum+=phi[i];
cout<<sum<<endl;
}
return 0;
}

The Euler function(hdoj --2824-欧拉函数)的更多相关文章

  1. hdu 2824(欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. HDU5597/BestCoder Round #66 (div.2) GTW likes function 打表欧拉函数

    GTW likes function      Memory Limit: 131072/131072 K (Java/Others) 问题描述 现在给出下列两个定义: f(x)=f_{0}(x)=\ ...

  3. hdu 2824 欧拉函数 O(nlogn) 和O(n)

    裸题 O(nlogn): #include <cstdio> #include <iostream> #include <algorithm> using name ...

  4. *HDU 1286,2824欧拉函数

    #include<iostream> #include<string> #include<cstdio> #include<cmath> #includ ...

  5. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

  6. hdu2824 The Euler function(欧拉函数个数)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/36426357 题目链接:h ...

  7. hdu 2824 The Euler function(欧拉函数)

    题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...

  8. hdu 2824 The Euler function 欧拉函数打表

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU - 2824 The Euler function 欧拉函数筛 模板

    HDU - 2824 题意: 求[a,b]间的欧拉函数和.这道题卡内存,只能开一个数组. 思路: ϕ(n) = n * (p-1)/p * ... 可利用线性筛法求出所有ϕ(n) . #include ...

  10. The Euler function(线性筛欧拉函数)

    /* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体 ...

随机推荐

  1. poj1101 the game 广搜

    题目大意: 类似于连连看,问从起点到终点最少需要几条线段. 规则: 1.允许出界. 2.空格的地方才能走. 分析: 题目做下来发现没有卡时间,所以主要还是靠思路.也就是说不用考虑离线算法.直接以每个起 ...

  2. OpenCV: OpenCV人脸检测框可信度排序

    参考文章:http://blog.csdn.net/hua_007/article/details/45368607 使用OpenCV进行人脸识别时,使用 casecade.detectMultiSc ...

  3. 多开 MFC线程

    序言:我才编程几年啊!就要处理多线程.对于只写函数的我,这难度简直了!不过MFC的多线程,貌似比较简单,还能处理的了. (1).开MFC多个线程 在视频采集的过程中,如果不使用媒体计数器,会造成主线程 ...

  4. 利用string 字符串拷贝

    序言:对于laws的代码,完全从Matlab中转来.其中用到了字符串复制和对比的函数. C++要求: 输入字符串,根据字符串,来确定选择数组,用于下一过程 MatLab代码: (1).文件calLaw ...

  5. 【技术累积】【点】【sql】【17】了解索引

    先上结论 数据库数据以平衡树进行聚合索引--主键的作用: 数据每行都存在叶子节点: 单独字段的索引,单独存在,且将该字段值取出: 单独字段的索引,查到对应的主键id,再通过聚合索引查到数据: 多字段索 ...

  6. layer自定义弹窗样式

    1.下载并引用js, 官网http://layer.layui.com/ 文档http://www.layui.com/doc/modules/layer.html <link href=&qu ...

  7. 怎样在PDF文件中查找某个特定的词?

    不得不说中国的修饰词太多了例如:“滚”可以这样说,请你以一种圆润的方式离开:上次小编在路上听到某男子打电话,好像是给女孩子,那口才,是真的牛,夸人不带重复的.要不是我男孩子,我都想以身相许了.人们常常 ...

  8. js-undefinde的一点延伸

    前面写过一篇js中变量定义的问题:Js中判断变量存不存在的问题 本文再补充下,变量声明未初始化的情况,代码: <script> var a; alert(a==undefined)//tr ...

  9. BZOJ 3510: 首都 LCT + multiset维护子树信息 + 树的重心

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000 using namespace std; ...

  10. PAT_A1151#LCA in a Binary Tree

    Source: PAT A1151 LCA in a Binary Tree (30 分) Description: The lowest common ancestor (LCA) of two n ...