Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】
Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】
标签: 入门讲座题解 数论
题目描述
Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.
Before reaching Amakusa's castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 ... b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.
He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains a line containing two integers a and b (1 ≤ a ≤ b < 231, b - a ≤ 100000).
Output
For each case, print the case number and the number of safe territories.
Sample Input
3
2 36
3 73
3 11
Sample Output
Case 1: 11
Case 2: 20
Case 3: 4
Note
A number is said to be prime if it is divisible by exactly two different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...
题意
给定\(l, r\),问区间\([l, \;r]\)内有几个质数?
解析
因为\(n\)较大,且区间长度最大为\(1e5\),所以不能直接使用朴素\(O(\sqrt{n})\)的方法来逐个判断区间的质数。
那么我们可以将埃氏筛的思想运用到区间上。用所有比\(r\)小的质数数来筛掉\([l, \;r]\)区间中的合数。用欧拉筛提前处理出\(\sqrt{N}\)以内的质数。然后用这些质数去筛掉\([l,\;r]\)区间的合数。这样这个算法的时间复杂度就是\(O(len\log \log{len})\)了。
quiz为什么只需要筛出\(\sqrt{N}\)以内的素数?
埃氏筛中,一个质数\(p\)所能筛掉的第一个合数为\(p \times p\)(\(p \times 2, p \times 3, p \times 4, \dots\) 都在之前被其他的质数筛掉了。)如果\(p\)能筛掉的最小合数都比\(N\)大,那么这个\(p\)就是用不到的,就不用筛出这个\(p\)来。
通过代码
/*
Problem
LightOJ - 1997
Status
Accepted
Time
105ms
Memory
15856kB
Length
1168
Lang
C++
Submitted
2019-11-25 19:11:37
RemoteRunId
1640684
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e7 + 50;
typedef long long ll;
bool vis[MAXN], _vis[100005];
int prime[MAXN / 10], cnt = 0;
void get_prime() //欧拉线性筛.先筛出sqrt(n)范围内的质数.
{
vis[1] = 1;
for(int i = 2; i <= int(1e6 + 5); i ++){
if(!vis[i])
prime[++ cnt] = i;
for(int j = 1; j <= cnt && i * prime[j] <= int(1e6 + 5); j ++){
vis[i * prime[j]] = 1;
if(i % prime[j] == 0)
break;
}
}
return;
}
int main()
{
int times, _case = 0;
get_prime();
scanf("%d", ×);
while(times --){
ll a, b;
int ans = 0;
memset(_vis, 0, sizeof(_vis));
scanf("%lld%lld", &a, &b);
if(a == 1) _vis[0] = 1; //如果a是1. 1不是质数,但无法通过我们的方法筛掉1.所以先手动筛掉.
for(int i = 1; i <= cnt && 1ll * prime[i] * prime[i] <= b; i ++) //用小于sqrt(b)的素数,开始筛掉[1, b]区间的合数.
for(ll j = max(1ll * prime[i] * prime[i], a / prime[i] * prime[i]); j <= b; j += prime[i]){ //找到落在区间内的,能被prime[i]筛掉的第一个合数.
if(j >= a && j > prime[i]) //要让j落在区间中,且j是合数.
_vis[j - a] = 1;
}
for(int j = 0; j < b - a + 1; j ++)
if(!_vis[j])
ans ++;
printf("Case %d: %d\n", ++ _case, ans);
}
return 0;
}
Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】的更多相关文章
- M - Help Hanzo LightOJ - 1197 (大区间求素数)
题意: 求[a,b]之间的素数的个数 数很大...数组开不起 所以要想到转化 因为小于等于b的合数的最小质因子 一定小于等于sqrt(b),所以只需要求出来[0,sqrt(b)]的素数 然后取倍数删 ...
- M - Help Hanzo LightOJ - 1197 (大区间素数筛法)
题解:素数区间问题.注意到a和b的范围是1<<31,所以直接暴力打表肯定不可以.如果一个数是合数,他的两个因子要么是两个sqrt(x),要么就分布在sqrt(x)两端,所以我们可以根据sq ...
- LightOJ 1197 Help Hanzo(区间素数筛选)
E - Help Hanzo Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...
- Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】
Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...
- Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】
Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...
- Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】
Goldbach`s Conjecture(LightOJ - 1259)[简单数论][筛法] 标签: 入门讲座题解 数论 题目描述 Goldbach's conjecture is one of t ...
- 简单实现图片间的切换动画 主要用到ViewPager
简单实现图片间的切换动画 主要用到ViewPagerViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view.ViewPager类需要一个PagerAdapter适 ...
- (step7.2.1)hdu 1395(2^x mod n = 1——简单数论)
题目大意:输入一个整数n,输出使2^x mod n = 1成立的最小值K 解题思路:简单数论 1)n可能不能为偶数.因为偶数可不可能模上偶数以后==1. 2)n肯定不可能为1 .因为任何数模上1 == ...
随机推荐
- 基于iSensor的MT9M001C12STM传感器调试总结
iSensor APP 之 摄像头调试 MT9M001C12STM iSensor app 非常适合调试各种摄像头,已测试通过的sensor有: l OV7670.OV7725.OV9650.OV ...
- 关于《iBoard 电子学堂》的学习及进阶方式(精 转)
关于<iBoard 电子学堂>的学习及进阶方式 <iBoard 电子学堂>自发布以来,受到广大网友的热烈关注.虽然我前期设计我花了大量精力,但能得到大家的认可,我也非常欣慰.由 ...
- 【CSS】357- 坚定地使用 CSS Custom Properties
自定义属性(Custom Properties)是一个很有魅力的 CSS 新特性,现代浏览器广泛 支持.但是遇到那些不支持 CSS Custom Properties 的老掉牙浏览器我们该怎么办?等着 ...
- 第三方OAuth授权登录,QQ、微信(WeChat)、微博、GitHub、码云(Gitee)、淘宝(天猫)、微软(Microsoft )、钉钉、谷歌(Google)、支付宝(AliPay)、StackOverflow
Netnr.Login 第三方OAuth授权登录 支持第三方登录 三方 参考文档 参考文档 参考文档 参考文档 参考文档 参考文档 参考文档 参考文档 参考文档 参考文档 参考文档 参考文档 安装 ( ...
- [译]C# 7系列,Part 9: ref structs ref结构
原文:https://blogs.msdn.microsoft.com/mazhou/2018/03/02/c-7-series-part-9-ref-structs/ 背景 在之前的文章中,我解释了 ...
- Asp.Net MVC中记录错误日志保存到本地txt文件
为了方便查询系统出错弄个错误日志出来对于维护运维来说是很有必要的. 1.在Asp.Net MVC项目中的App_Start添加一个用于处理异常类的文件ErrorLog让他继承HandleErrorAt ...
- [ASP.NET Core 3框架揭秘] 依赖注入[6]:服务注册
通过<利用容器提供服务>我们知道作为依赖注入容器的IServiceProvider对象是通过调用IServiceCollection接口的扩展方法BuildServiceProvider创 ...
- ubuntu1604环境下mariadb启动卡住报错和apparmor基本使用
问题描述:Ubuntu 1604 新环境下使用apt安装的mariadb10版本,结果第二天就起不来了,很是郁闷 启动时会卡住,当时就慌了,这什么情况啊,昨天好好的今天就起不来了,过了一会儿就有返回信 ...
- centos7配置阿里yum源
首先刚刚安装完的centos并不像Ubuntu系统那样有很多的源来可以供您使用 所以我们需要通过下载阿里云的yum源在下载epel-release.noarch扩展包就可以了 操作如下: 1.首先这是 ...
- canves做的时钟目前已经开源
canves做的时钟目前已经开源 git地址: https://github.com/jidanji/canves-clock/tree/1.0.1 项目截图 时流过的时间变得有颜色,其他的没有颜色.