zoukankan      html  css  js  c++  java
  • P5535 【XR-3】小道消息

    题解

    根据切比雪夫定理,对于整数 (x>1),至少有一个质数 (p) 满足 (x<p<2x)

    所以第一天会从数字为 (k+1) 的人传到某个满足 (lfloor frac{n}{2} floor<p<n) 的质数 (p),第二天会从这个质数传到所有数。

    考虑什么情况下只需要一天:(2sim (n+1)) 中所有数(除了 (k+1))都与 (k+1) 互质,即 (k+1) 为质数且 (2(k+1)>n+1)

    一个更强的说法:对于任意整数 (n>3),都有质数 (p) 满足 (n<p<2n-2)

    代码
    #include <cstdio>
    #include <cstring>
    #include <cctype>
    using namespace std;
    #define For(Ti,Ta,Tb) for(int Ti=(Ta);Ti<=(Tb);++Ti)
    #define Dec(Ti,Ta,Tb) for(int Ti=(Ta);Ti>=(Tb);--Ti)
    template<typename T> void Read(T &x){
    	x=0;int _f=1;
    	char ch=getchar();
    	while(!isdigit(ch)) _f=(ch=='-'?-1:_f),ch=getchar();
    	while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    	x=x*_f;
    }
    template<typename T,typename... Args> void Read(T &x,Args& ...others){
    	Read(x);Read(others...);
    }
    typedef long long ll;
    const int Inf=0x3f3f3f3f;
    ll n,k;
    bool IsPrime(ll x){
    	for(ll i=2;i*i<=x;++i) if(x%i==0) return 0;
    	return 1;
    }
    int main(){
    	Read(n,k);
    	puts(IsPrime(k+1)&&(k+1)*2>n+1?"1":"2");
    	return 0;
    }
    
    Written by Alan_Zhao
  • 相关阅读:
    yzoj P2344 斯卡布罗集市 题解
    yzoj P2350 逃离洞穴 题解
    yzoj P2349 取数 题解
    JXOI 2017 颜色 题解
    NOIP 2009 最优贸易 题解
    CH 4302 Interval GCD 题解
    CH4301 Can you answer on these queries III 题解
    Luogu2533[AHOI2012]信号塔
    Luogu3320[SDOI2015]寻宝游戏
    Luogu3187[HNOI2007]最小矩形覆盖
  • 原文地址:https://www.cnblogs.com/alan-zhao-2007/p/p5535-sol.html
Copyright © 2011-2022 走看看