The Euler function 欧拉函数打表

本文介绍了一种基于欧拉函数的快速计算方法,用于解决特定区间内欧拉函数值的累加问题。通过预处理大量数值的欧拉函数值,并采用高效的筛选策略,实现了在限定时间内对指定范围内的欧拉函数进行求和。

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

The Euler function

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


回顾一下欧拉函数的性质:小于或等于n的与n互质的整数数量为x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn)

                                            n互质的所有数的和sum=n*[φ(n)/2]

一个数一个数的判断会超时,可以用类似于素数筛选的方法打表

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
using namespace std;
#define maxn 3000010
long long phi[maxn+1];
void Euler() {
    long long a,b,i,j;
    for(i=1; i<=maxn; i++) phi[i]=i;
    for(i=2; i<=maxn; i+=2) phi[i]/=2;
    for(i=3; i<=maxn; i+=2)
        if(phi[i]==i) {
            for(j=i; j<=maxn; j+=i)
                phi[j]=phi[j]-phi[j]/i;
        }
}

int main() {
    Euler();
    long long a,b;
    while(cin>>a>>b) {
        long long sum=0; //  sum的类型要为long long 不然会WA
        for(int i=a; i<=b; i++)
            sum+=phi[i];
        cout<<sum<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值