您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: