The Euler function(hdoj --2824-欧拉函数)
2024-09-06 14:43:38
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)
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;
}
最新文章
- 微信5.0之Fragment使用
- Facebook开源动画库 POP-POPDecayAnimation运用
- linux 查看僵尸进程
- 创建动态组-以OU为单位
- 关于javac编译时出现“非法字符:\65279”的解决方法
- IE浏览器Ajax缓存问题小结
- System.in.read()
- 实现自己的cp命令
- 类型转换操作符static_cast、const_cast、dynamic_cast、reinterpret_cast
- self parent $this关键字分析--PHP
- javascript统计输入文本的简易方法
- CodeForces 670 A. Holidays(模拟)
- Java 故障安全异常处理
- HTML5 新增的 input 事件
- Redirect all output to file
- shell基础语法以及监控进程不存在重新启动
- DRL前沿之:Benchmarking Deep Reinforcement Learning for Continuous Control
- 也许,这样理解HTTPS更容易(今天看到的, 对https总结最好的一篇)
- 算法笔记_045:币值最大化问题(Java)
- 20145303刘俊谦 Java 代码托管
热门文章
- P1401 城市(30分,正解网络流)
- Java 开源博客 Solo 1.2.0 发布 - 一键启动
- 【Oracle】rollup函数
- ORA-03113 ---end-of-file on communication channel 解决方案记录
- System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖
- 使用OpenCV画折线图
- CVPR2015深度学习回顾
- 利用string 字符串拷贝
- PAT_A1111#Online Map
- atcoder.keyence2019.contest E-Connecting Cities