您的位置:首页 > 其它

HDU2824 The Euler function

2016-07-11 20:57 316 查看

 

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

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

2009 Multi-University Training Contest 1 - Host by TJU

 

算出300w个范围内的欧拉函数,然后求个前缀和。

然而这题内存限制好小,直接套惯用的模板会MLE。

已经没有什么优化的空间了,只好重构代码。

 

解1:MLE

#include <cstdio>
long long n=3000000,phi[3000001],p[1500001],top=0;
bool ma[3000001];
void init()
{
phi[1]=1;
for (int i=2;i<=n;++i)
{
if (!ma[i]) ma[i]=true,p[++top]=i,phi[i]=i-1;
for (int j=1;j<=top&&i*p[j]<=n;++j)
{
ma[i*p[j]]=true;
if (i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;}
else phi[i*p[j]]=(p[j]-1)*phi[i];
}
}
}
int main()
{
int l,r; init();
for (int i=1;i<=n;++i) phi[i]+=phi[i-1];
while (~scanf("%d%d",&l,&r)) printf("%lld\n",phi[r]-phi[l-1]);
}
View Code

 

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