【题解】UVA10140 [Prime Distance]

【题解】UVA10140 Prime Distance

哈哈哈哈\(miller-rabbin\)水过去了哈哈哈

还能怎么办呢?\(miller-rabbin\)直接搞。枚举即可,还跑得飞快。

o_2019-03-09%2015-20-43%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png

当然此题由于\(20000^2 >2^{31}\),直接预处理\(20000\)内的质数就好了

放mr的代码

#include<bits/stdc++.h>

using namespace std;typedef long long ll;
#define DRP(t,a,b) for(register int t=(a),edd=(b);t>=edd;--t)
#define RP(t,a,b)  for(register int t=(a),edd=(b);t<=edd;++t)
#define ERP(t,a)   for(register int t=head[a];t;t=e[t].nx)
#define midd register int mid=(l+r)>>1
#define TMP template < class ccf >
#define lef l,mid,pos<<1
#define rgt mid+1,r,pos<<1|1
#define pushup(pos) (seg[pos]=seg[pos<<1]+seg[pos<<1|1])
TMP inline ccf qr(ccf b){
    register char c=getchar();register int q=1;register ccf x=0;
    while(c<48||c>57)q=c==45?-1:q,c=getchar();
    while(c>=48&&c<=57)x=x*10+c-48,c=getchar();
    return q==-1?-x:x;}
TMP inline ccf Max(ccf a,ccf b){return a<b?b:a;}
TMP inline ccf Min(ccf a,ccf b){return a<b?a:b;}
TMP inline ccf Max(ccf a,ccf b,ccf c){return Max(a,Max(b,c));}
TMP inline ccf Min(ccf a,ccf b,ccf c){return Min(a,Min(b,c));}
TMP inline ccf READ(ccf* _arr,int _n){RP(t,1,_n)_arr[t]=qr((ccf)1);}
//----------------------template&IO---------------------------
#define int long long
inline int ksm(ll base,ll p,ll mod){register int ret=1;base%=mod;
    for(register ll t=p;t;t>>=1,(base*=base)%=mod) if(t&1) (ret*=base)%=mod;
    return ret%mod;
}

inline bool T(int base,int p){
    for(register int t=p-1,sav;t;t>>=1){
    sav=ksm(base,t,p);
    if(sav!=1&&sav!=p-1) return 0;
    if((t&1)||sav==p-1) return 1;
    }return 1;
}

inline bool mb(int x){
    if(x==2||x==3||x==13||x==17) return 1;
    if(x==1) return 0;
    return  T(2,x)&&T(3,x)&&T(13,x)&&T(17,x);
}

int L,R,last,Mx,Mn,ans1,ans2,ans3,ans4;
main(){
    while(cin>>L>>R){
    Mx=0;
    Mn=R-L+2;last=R+1;
    RP(t,L,R) if(mb(t)){ last=t; break;}
    if(last==R+1) {printf("There are no adjacent primes\n"); continue;}
    RP(t,last+1,R){
        if(mb(t)){
        if(t-last>Mx) Mx=t-last,ans3=last,ans4=t;
        if(t-last<Mn) Mn=t-last,ans1=last,ans2=t;
        last=t;
        }
    }
    if(Mx==0||Mn==R-L+2) printf("There are no adjacent primes.\n");
    else printf("%d,%d are closest, %d,%d are most distant.\n",ans1,ans2,ans3,ans4);
    }
    return 0;
}

转载于:https://www.cnblogs.com/winlere/p/10500966.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值