The Euler function

本文介绍了一种计算区间内欧拉函数之和的有效算法,通过预处理得到每个数的欧拉函数值,进而快速求解任意区间[a, b]内所有数的欧拉函数值之和。

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

题目描述:

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

思路:就是求一个区间内的欧拉函数之和,

给出一个结论:

 设E( x)为X的欧拉函数,p为质数

对于E(x*p)  

if   p是x的因数  E(x*p)=E(x)*p;

else  E (x*P)=E(x)*(p-1);

 

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxv=3e6+10;
int v[3000005],a[3000006],b[3000005];//b数组就是欧拉函数
void init(){
	for(int i=2;i<3000006;i++){
		if(!v[i]){
			a[++a[0]]=i;
			b[i]=i-1; 
		}
		for(int j=1;j<=a[0]&&i*a[j]<3000006;j++){
			v[i*a[j]]=1;
			if(i%a[j]==0){
				b[i*a[j]]=b[i]*a[j];
				break;
			}else{
				b[i*a[j]]=b[i]*(a[j]-1);
			}
		}
	}
} 
int main(){
	int n,m,len;
	init();
	long long sum=0;
	cin>>n>>m;
	if(n>m)swap(n,m);
	for(int i=n;i<=m;i++)sum+=b[i];
	cout<<sum;
	
}

 

 

 

 

### 显式Euler方法在MATLAB中的实现 显式Euler方法是一种用于求解常微分方程(ODE)数值近似的一阶迭代算法。该方法简单易懂,在许多情况下可以作为初学者学习数值分析的良好起点。 对于给定的初始条件 \( y(t_0)=y_0 \),以及定义于区间\[t_0, t_n\]上的函数\(f(t,y)\),通过离散化时间轴并采用固定步长h,可得如下更新公式: \[ y_{n+1} = y_n + h * f(t_n, y_n) \] 其中\(y_{n}\)表示第n个时间节点处未知变量的估计值[^1]。 下面是一个简单的Matlab代码片段来演示如何利用此方法解决一阶线性ODE: ```matlab function [T,Y]=euler(f,tspan,y0,n) % EULER Explicit Euler Method to solve ODEs. % % Inputs: % - f : function handle of the form dydt=f(t,y). % - tspan : two-element vector specifying start and end times. % - y0 : initial condition at time=tspan(1). % - n : number of steps. h=(tspan(2)-tspan(1))/n; % Step size calculation T=linspace(tspan(1),tspan(2),n+1); % Time points generation Y=zeros(size(T)); Y(1)=y0;% Initialize solution array with IC for i=1:n k1=f(T(i),Y(i)); Y(i+1)=Y(i)+h*k1; end ``` 上述程序接受四个参数输入:被积函数`f`、积分范围`tspan`、起始状态`y0`和分割数目`n`。它返回两个向量——时间和对应的数值解序列[T,Y][^2]。 为了更好地理解这个过程,考虑一个具体的例子,比如计算指数衰减模型\(dy/dt=-ay\)从时刻零到五秒内的响应曲线,假设a等于0.5且初始浓度为1mol/L,则调用方式如下所示: ```matlab >> a=@(t,y)(-0.5*y); >> [T,Y]=euler(a,[0 5],1,100); >> plot(T,Y,'r') >> xlabel('Time (sec)') >> ylabel('Concentration C(t)') >> title('Exponential Decay using Forward Euler Method') ``` 这段脚本绘制出了随时间变化而减少的物质浓度图象,直观展示了Forward Euler法的应用效果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值