2824 The Euler function【欧拉函数】

本文探讨了欧拉函数的定义、性质及如何高效计算区间内所有欧拉函数的和,通过实例演示了该计算过程及应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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
 


题意:

给出两个数,让你求出位于这两个数之间的所有数的欧拉函数值的和。


给出的时间比较短,求出所有的函数值,然后相加是不明智的,还是打欧拉函数表比较好,但是问题解决了吗?没有,因为依然是超时!

怎么解决?哈哈,难道不能打一个每一项都是前面各项的累加和的欧拉函数值表吗??

欧拉函数,自己掌握的也不好,勉强能敲出来模板,慢慢学习吧!


#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;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值