素数占比

在这里插入图片描述在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<cmath>
int n;
long long l,r;
using namespace std;
int main()
{
	while(scanf("%d",&n) !=EOF){
		while(n--){
			cin>>l>>r;
			if(l>r)
				swap(l,r);
			int x=0;
			for(int i=l;i<=r;i++){
				int m=0;
				for(int j=2;j<=sqrt(i);j++){
					if(i%j==0)
						m+=1;
				}
				if(!m)
					x+=1;
				else
				continue;
			}
			if(double(x)/(r-l+1)<(double(1)/3))
				cout<<"Yes"<<endl;
			else
				cout<<"No"<<endl;
		}
	}
	return 0;
}

运算量过大,超出时间限制
1到 n 的质数个数数量级在n/ ln n,由质数的稀疏性,可以验证当区间长度大于100时答案一定是 Yes 了,而小于100时直接枚举就很简单了:

#include<iostream>
#include<cstdio>
#include<cmath>
int n;
long long l,r;
using namespace std;
int main()
{
	while(scanf("%d",&n) !=EOF){
		while(n--){
			cin>>r>>l;
			if(l>r)
				swap(l,r);
			if(r-l+1>100){
				cout<<"Yes"<<endl;
			}
			else{
				int x=0;
				for(int i=l;i<=r;i++){
					int m=0;
					for(int j=1;j<=floor(sqrt(i));j++){
						if(i%j==0)
							m+=1;
					}
					if(m==1)
						x+=1;	
				}
				if(3*x<(r-l+1))
					cout<<"Yes"<<endl;
				else
					cout<<"No"<<endl;
			}
		}		
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值