2824 The Euler function【欧拉函数】
2015-08-24 18:29
330 查看
The Euler function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4507 Accepted Submission(s): 1872
[align=left]Problem Description[/align]
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)
[align=left]Input[/align]
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
[align=left]Output[/align]
Output the result of (a)+ (a+1)+....+ (b)
[align=left]Sample Input[/align]
3 100
[align=left]Sample Output[/align]
3042
题意:
给出两个数,让你求出位于这两个数之间的所有数的欧拉函数值的和。
给出的时间比较短,求出所有的函数值,然后相加是不明智的,还是打欧拉函数表比较好,但是问题解决了吗?没有,因为依然是超时!
怎么解决?哈哈,难道不能打一个每一项都是前面各项的累加和的欧拉函数值表吗??
欧拉函数,自己掌握的也不好,勉强能敲出来模板,慢慢学习吧!
#include<cstdio>
#define maxn 3000005
long long x[maxn];
void db()//打表
{
int i,j;x[1]=0;
for(i=2;i<maxn;++i)
{
if(i&1)
{
x[i]=i;
}
else
{
x[i]=i/2;
}
}
x[2]+=x[1];
for(i=3;i<maxn;i+=2)
{
if(x[i]==i)
{
for(j=i;j<maxn;j+=i)//计算
{
x[j]=x[j]/i*(i-1);
}
}
x[i]+=x[i-1];x[i+1]+=x[i];//计算累加和
}
}
int main()
{
int n,m;
db();
while(~scanf("%d%d",&n,&m))
{
printf("%lld\n",x[m]-x[n-1]);//注意输出,为什么自己想想
}
return 0;
}
相关文章推荐
- 2010到2015,我的6年投资理财经验总结和反思
- 2010到2015,我的6年投资理财经验总结和反思
- 野人学Android基础篇之初探UI控件第一课--TextView静态赋值
- 初学软工——需求分析
- python中的read()、readline()与readlines()
- 2010到2015,我的6年投资理财经验总结和反思
- 最短路 POJ2387
- 发布MQTT消息究竟有多快?
- 数组中的元素排序问题
- 学习笔记——Java调用Oracle存储过程
- 基于动态阈值的白平衡算法 照片校色
- Linux磁盘占用100%解决方法
- 楼宇及工业自动化总线协议介绍
- 数据库-事务
- sql语句关键字over的作用及用法
- Bacula服务端的安装
- ArrayList与LinkedList大比拼之add和remove
- poj 2135 最小费用最大流模板题
- R语言-差分要注意的问题
- 图表控件TeeChart干货分享(绘制2D、3D实时曲线---VC++示例源代码--网络首发)