The Euler function

本文探讨了如何使用筛法欧拉函数计算指定区间内的欧拉函数和,详细介绍了欧拉函数的基本性质及其在求解特定数学问题中的应用。通过具体的例子和步骤演示,帮助读者理解并掌握这一算法。

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

Link:http://acm.hdu.edu.cn/showproblem.php?pid=2824

The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3889    Accepted Submission(s): 1613


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
 

Source
 



大概:求a~b段欧拉值和,要用到筛法欧拉函数。

筛选:1)如果n为素数,φ(n)=n-1;

           2)如果m,n互质,φ(n*m)=φ(n)*φ(m);


核心就是当m%p==0:

f(m*p) = f(m) * p;//因为(p-1)是在f(m)中所以是乘p;

否则:

f(m*p) = f(m) * (p-1);

因为Euler函数是积性函数所以


综合上面两个式子就可以知道核心的推导公式如何得来


   

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<ctime>  
  4. using namespace std;  
  5. typedef  __int64 INT;  
  6. #define N 3000001  
  7. int prime[216818],phi[N];  
  8. bool a[N];  
  9. void is_prime()  
  10. {  
  11.     int i,j,k;  
  12.     k=0;  
  13.     for(i=2;i<N;i++)  
  14.     {  
  15.        if(!a[i])  
  16.        {  
  17.           prime[k++]=i;  
  18.           phi[i]=i-1;  
  19.        }  
  20.        for(j=0;j<k&&i*prime[j]<N;j++)  
  21.        {  
  22.          a[prime[j]*i]=1;  
  23.          if(i%prime[j])  
  24.              phi[i*prime[j]]=phi[i]*(prime[j]-1);  
  25.          else  
  26.          {  
  27.              phi[i*prime[j]]=phi[i]*prime[j];  
  28.              break;  
  29.          }  
  30.          
  31.        }  
  32.       
  33.     }  
  34. }  
  35. int main()  
  36. {  
  37.     //freopen("c_in.txt","r",stdin);  
  38.     //freopen("myout5.txt","w",stdout);  
  39.     int a,b,i;  
  40.     INT sum;  
  41.     is_prime();  
  42.     while(cin>>a>>b)  
  43.     {  
  44.         sum=0;  
  45.         for(i=a;i<=b;i++)  
  46.             sum+=phi[i];  
  47.         printf("%I64d\n",sum);  
  48.       
  49.     }  
  50.     //printf("%lfMS\n",(double)clock()/CLOCKS_PER_SEC);  
  51.     return 0;  
  52. }  
### 显式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、付费专栏及课程。

余额充值