1. 程式人生 > >HIHO #1298 : 數論五·尤拉函式

HIHO #1298 : 數論五·尤拉函式

題目連結

#include<bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define fastIO ios::sync_with_stdio(false);cin.tie(0);
#define LL long long
#define pb push_back
#define gcd __gcd

#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)
const double EPS = 1e-8; const int maxn = 5e6+10; const int inf = 1 << 28; /* [0,n]之間的尤拉函式值 */ int phi[maxn]; void getPhi(int n){ cl(phi,0);phi[1]=1; for(int i=2;i<=n;i++)if(!phi[i]){ for(int j=i;j<=n;j+=i){ if(!phi[j])phi[j]=j; phi[j]=phi[j]/i*(i-1); } } } int
main(){ int L,R; cin>>L>>R; getPhi(R); int ans = R,val = R; for(int i=L;i<=R;i++){ if(phi[i]<val){ val = phi[i]; ans = i; } else if(phi[i]==val){ if(i<ans)ans = i; } } cout<<ans<<endl; return
0; }