P1865 A % B Problem

题目背景
题目名称是吸引你点进来的

实际上该题还是很水的

题目描述
区间质数个数

输入格式
一行两个整数 询问次数n,范围m

接下来n行,每行两个整数 l,r 表示区间

输出格式
对于每次询问输出个数 t,如l或r∉[1,m]输出 Crossing the line

输入输出样例
输入 #1复制
2 5
1 3
2 6
输出 #1复制
2
Crossing the line
说明/提示
【数据范围和约定】

对于20%的数据 1<=n<=10 1<=m<=10

对于100%的数据 1<=n<=1000 1<=m<=1000000 -109<=l<=r<=109 1<=t<=1000000

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iostream>
#include<algorithm>
#include<ctime>
#define ll int
#define dd double
#define f(i, x, y) for(ll i = x; i < y; i++)
//srand(time(NULL));
ll a[1000005], b[1000005];
using namespace std;
void sushu() {
	for (ll i = 4; i <= 1000005; i += 2) {
		a[i] = 1;
	}
	for (ll i = 2; i <= 1000005; i++) {
		if (a[i] == 1) {
			continue;
		}
		else {
			for(ll j = i + i; j < 1000005; j = j + i) {
				a[j] = 1;
			}
		}
	}
}
int main() {
	ll n, m; cin >> n >> m;
	f(i, 0, 1000005) {
		a[i] = 0;
		b[i] = 0;
	}
	b[0] = 0;
	a[0] = 1;
	a[1] = 1;
	sushu();
	f(i, 0, 1000005) {
		if (a[i] == 0) {
			b[i] = b[i - 1] + 1;
		}
		else {
			b[i] = b[i - 1];
		}
	}
	f(i, 0, n) {
		ll l, r; cin >> l >> r;
		if (l < 1 || r > m) {
			cout << "Crossing the line" << endl;
			continue;
		}
		if (a[r] == 0 && a[l] == 0) {
			cout << b[r] - b[l - 1]<< endl;
		}
		else {
			cout << b[r] - b[l - 1] << endl;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值