1 条题解
-
0陈烨鑫 (chenyexin) LV 10 @ 2023-4-22 12:31:50
简简单单AC代码
#include <iostream> #include <math.h> #include <stdio.h> #include <string.h> using namespace std; const int AX = 5e4; const int maxn = 1e6+666; const int INF = 1e9; int vis[AX]; int f[maxn]; int prime[AX]; int ans; void prime1(){ //筛选出50000以内的素数 ans = 0; memset(vis,0,sizeof(vis)); int q = (int)sqrt(AX+0.5); for(int i=2;i<=q;i++){ if(!vis[i]){ for(int j= i*i;j<AX;j+=i){ vis[j] = 1; } } } for(int i=2;i<AX;i++){ if(!vis[i]){ prime[ans++] = i; } } } void prime_2(int l,int u){ //求l,u合数 int a,b; for(int i=0;i<ans;i++){ a = (l-1)/prime[i]+1; b = u/prime[i]; for(int j=a;j<=b;j++){ if(j>1) f[j*prime[i]-l] = 1; //偏移减去l,后面再加回来。 } } } int main(){ long long l,u; prime1(); while(cin>>l>>u){ memset(f,0,sizeof(f)); if(l == 1) l = 2; prime_2(l,u); int mi = INF; int ma = -1; int p = -1; int x1,x2,y1,y2; for(int i=0;i<=u-l;i++){ //枚举50000以内素数 if(!f[i]){ if(p == -1) {p=i;continue;} if(mi > i-p) mi = i-p,x1=p+l,y1=i+l; //偏移量加回来 if(ma < i-p) ma = i-p,x2=p+l,y2=i+l; p = i; } } if(ma == -1) printf("There are no adjacent primes.\n"); else cout<<x1<<","<<y1<<" are closest, "<<x2<<","<<y2<<" are most distant."<<endl; } return 0; }
- 1
信息
- ID
- 107
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 3
- 标签
- 递交数
- 24
- 已通过
- 20
- 上传者