CF937B题解

原题传送门

solution 1

看到题目,我是一开始想(我太弱了)先预处理出 \(2\sim p\) 的倍数中所有 \(\leqslant \text{y}\) 的数,但时间复杂度是 \(\mathcal O (py)\),最差 \(\mathcal O ((10^9)^2) = \mathcal O (10^{18})\) 明显超时。。。

for(int i=2;i<p;i++){
        for(int j=i;j<y;j+=i){
            num[j]=1;
        }
}

solution 2

前置芝士

求 x 是否是质数:

定义 \(n = \left \lfloor \sqrt{x}\right\rfloor\)

则如果在 \(\left[2,n\right]\) 中没有 \(n\) 的因数 \(\left[2,x\right]\) \(2\)\(x\) 中没有 \(x\) 的因数。

证明:由于一个数的因数是对应的,

\(a \mid n\)\((n \div a)\mid n\)

所以若 \(\left[2,n\right]\) 没有其因数,它就是素数,证毕。

思路

根据前置芝士,我们可以推断出若 \([2,\sqrt{y}]\)中没有\([2,p]\)的任意一个数的倍数,则\([2,y]\)中没有\([2,p]\)的任意一个数的倍数。

code

int p,y;
    cin>>p>>y;
    bool a=0;
    for(int i=y;i>p;i--){
        a=0;
        for(int j=2;j<=sqrt(i)&&j<=p;j++){
            if(i%j==0){
                a=1;
                break;
            }
        }
        if(!a){
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<-1;

the end.

posted @ 2021-07-30 20:29  WiccldCute  阅读(86)  评论(0)    收藏  举报