B - Help Hanzo (LightOJ - 1197)

- 题目大意

在一个区间中去寻找素数的个数。

- 解题思路

由于a,b的取值范围比较大,无法把这个区间内的所以素数全部筛选出来,但是b-a这个区间比较小,所以可以用区间素数筛选的办法解决这个题目。

- 代码

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int MAX = 1e6 + 5;
bool vis[MAX],vis2[MAX];
int cnt = 0;
int p[MAX];
void num()
{

	memset(vis, 0, sizeof(vis));
	vis[1] = 1;
	for (int i = 2; i <MAX; i++)
	{
		if (!vis[i])
		{
			p[cnt++] = i;
			for (long long j = i*2; j <= MAX; j += i)
			{
				vis[j] = 1;
			}
		}
	}
}
int main()
{
	long long n, a, b;
	cin >> n;
	num();
	for (long long  i = 1; i <= n; i++)
	{
		long long sum = 0;
		cin >> a >> b;
		if (b <= MAX - 1)
		{
			for (long long i = a; i <= b; i++)
			{
				if (!vis[i])
					sum++;
			}
		}
		else
		{
			memset(vis2, 0, sizeof(vis2));
			for (int i = 0; i<cnt&&p[i] <= b; i++)
			{
				long long k = a / p[i];
				if (k*p[i]<a)
					k++;
				for (long long j = k * p[i]; j <= b; j += p[i])
				{
					vis2[j - a] = 1;
				}
			}
			for (long long i = a; i <= b; i++)
			{
				if (!vis2[i - a])
					sum++;
			}
		}
		cout << "Case " << i << ": ";
		cout << sum << endl;
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/alpacadh/p/8448333.html

时间: 2024-10-08 23:52:29

B - Help Hanzo (LightOJ - 1197)的相关文章

(最长公共子序列+推导)Love Calculator (lightOJ 1013)

http://www.lightoj.com/volume_showproblem.php?problem=1013 Yes, you are developing a 'Love calculator'. The software would be quite complex such that nobody could crack the exact behavior of the software. So, given two names your software will genera

(状压) Marriage Ceremonies (lightOJ 1011)

http://www.lightoj.com/volume_showproblem.php?problem=1011 You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job is quite hard for you. The job gets more difficult when people come here and give their bi

(LightOJ 1149) Factors and Multiples

题目链接:http://lightoj.com/volume_showproblem.php?problem=1149 Description You will be given two sets of integers. Let's call them set A and set B. Set A contains n elements and set B contains m elements. You have to remove k1 elements from set A and k2

(LightOJ 1004) Monkey Banana Problem 简单dp

You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). Wh

LightOj 1197 Help Hanzo (区间素数筛选)

题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围比较大,无法把这个区间内的所以素数全部筛选出来,但是b-a这个区间比较小,所以可以用区间素数筛选的办法解决这个题目. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<

LightOJ 1205 - Palindromic Numbers (数位dp)

LightOJ 1205 - Palindromic Numbers (数位dp) ACM 题目地址:SPOJ MYQ10 Mirror Number 题意: 求[a,b]中回文的个数. 分析: 是SPOJ MYQ01的简单版...其实有非递归方法的. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.net/hcbbt * File: 1205.cpp * Create Date: 2014-08-

LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路:根据唯一分解定理,把X写成若干素数相乘的形式,则X的正因数的个数为:(1+a1)(1+a2)(1+a3)...(1+an).(ai为指数) 因为这道题目是求矩形,所以知道一个正因数后,另一个正因数也就确定了,所以每组正因数重复计算了两遍,需要除以2. 最后减去小于b的因数. 1 #include<

LightOJ - 1246 Colorful Board(DP+组合数)

http://lightoj.com/volume_showproblem.php?problem=1246 题意 有个(M+1)*(N+1)的棋盘,用k种颜色给它涂色,要求曼哈顿距离为奇数的格子之间不能涂相同的颜色,每个格子都必须有颜色,问可行的方案数. 分析 经一波分析,根据曼哈顿距离为奇数这一信息,可以将棋盘分为两部分,也就是相邻格子不能有相同颜色.一种颜色只能在一个部分中出现.现在考虑对一个部分的格子操作, dp[i][j]表示i个格子选择用了j种颜色的方案数,于是可以得到这样的递推式:

LightOJ 1197 Help Hanzo(区间素数筛法)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std; #define maxn 50000 int vis[maxn], isprime[5200], num[100005], k; void prime()//只需要把[1,sqrt(2^31)]之间的素数筛选出来就ok了. {