Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】

标签: 入门讲座题解 数论


题目描述

  1. 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.
  2. 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.
  3. 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

  1. Input starts with an integer T (≤ 200), denoting the number of test cases.
  2. Each case contains a line containing two integers a and b (1 a b < 231, b - a 100000).

Output

  1. For each case, print the case number and the number of safe territories.

Sample Input

  1. 3
  2. 2 36
  3. 3 73
  4. 3 11

Sample Output

  1. Case 1: 11
  2. Case 2: 20
  3. Case 3: 4

Note

  1. 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\)来。


通过代码

  1. /*
  2. Problem
  3. LightOJ - 1997
  4. Status
  5. Accepted
  6. Time
  7. 105ms
  8. Memory
  9. 15856kB
  10. Length
  11. 1168
  12. Lang
  13. C++
  14. Submitted
  15. 2019-11-25 19:11:37
  16. RemoteRunId
  17. 1640684
  18. */
  19. #include <bits/stdc++.h>
  20. using namespace std;
  21. const int MAXN = 1e7 + 50;
  22. typedef long long ll;
  23. bool vis[MAXN], _vis[100005];
  24. int prime[MAXN / 10], cnt = 0;
  25. void get_prime() //欧拉线性筛.先筛出sqrt(n)范围内的质数.
  26. {
  27. vis[1] = 1;
  28. for(int i = 2; i <= int(1e6 + 5); i ++){
  29. if(!vis[i])
  30. prime[++ cnt] = i;
  31. for(int j = 1; j <= cnt && i * prime[j] <= int(1e6 + 5); j ++){
  32. vis[i * prime[j]] = 1;
  33. if(i % prime[j] == 0)
  34. break;
  35. }
  36. }
  37. return;
  38. }
  39. int main()
  40. {
  41. int times, _case = 0;
  42. get_prime();
  43. scanf("%d", &times);
  44. while(times --){
  45. ll a, b;
  46. int ans = 0;
  47. memset(_vis, 0, sizeof(_vis));
  48. scanf("%lld%lld", &a, &b);
  49. if(a == 1) _vis[0] = 1; //如果a是1. 1不是质数,但无法通过我们的方法筛掉1.所以先手动筛掉.
  50. for(int i = 1; i <= cnt && 1ll * prime[i] * prime[i] <= b; i ++) //用小于sqrt(b)的素数,开始筛掉[1, b]区间的合数.
  51. for(ll j = max(1ll * prime[i] * prime[i], a / prime[i] * prime[i]); j <= b; j += prime[i]){ //找到落在区间内的,能被prime[i]筛掉的第一个合数.
  52. if(j >= a && j > prime[i]) //要让j落在区间中,且j是合数.
  53. _vis[j - a] = 1;
  54. }
  55. for(int j = 0; j < b - a + 1; j ++)
  56. if(!_vis[j])
  57. ans ++;
  58. printf("Case %d: %d\n", ++ _case, ans);
  59. }
  60. return 0;
  61. }

Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】的更多相关文章

  1. M - Help Hanzo LightOJ - 1197 (大区间求素数)

    题意: 求[a,b]之间的素数的个数 数很大...数组开不起 所以要想到转化 因为小于等于b的合数的最小质因子 一定小于等于sqrt(b),所以只需要求出来[0,sqrt(b)]的素数  然后取倍数删 ...

  2. M - Help Hanzo LightOJ - 1197 (大区间素数筛法)

    题解:素数区间问题.注意到a和b的范围是1<<31,所以直接暴力打表肯定不可以.如果一个数是合数,他的两个因子要么是两个sqrt(x),要么就分布在sqrt(x)两端,所以我们可以根据sq ...

  3. LightOJ 1197 Help Hanzo(区间素数筛选)

    E - Help Hanzo Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  4. Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

    Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...

  5. Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】

    Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...

  6. Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】

    Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...

  7. Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】

    Goldbach`s Conjecture(LightOJ - 1259)[简单数论][筛法] 标签: 入门讲座题解 数论 题目描述 Goldbach's conjecture is one of t ...

  8. 简单实现图片间的切换动画 主要用到ViewPager

    简单实现图片间的切换动画 主要用到ViewPagerViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view.ViewPager类需要一个PagerAdapter适 ...

  9. (step7.2.1)hdu 1395(2^x mod n = 1——简单数论)

    题目大意:输入一个整数n,输出使2^x mod n = 1成立的最小值K 解题思路:简单数论 1)n可能不能为偶数.因为偶数可不可能模上偶数以后==1. 2)n肯定不可能为1 .因为任何数模上1 == ...

随机推荐

  1. 模拟摄像头解码模块最新测试 TVP5150模块 FPGA+SDRAM+TVP5150+VGA 实现PAL AV输入 VGA视频输出

    模拟摄像头解码模块最新测试  TVP5150模块  FPGA+SDRAM+TVP5150+VGA  实现PAL AV输入 VGA视频输出 测试使用电视机顶盒的AV模拟信号输入,VGA显示器输出测试,效 ...

  2. 新更新的OV7670 OV7725模块效果展示 OV7670 FPC版 30万像素 CMOS模块 兼容官哥方便 FPGA stm32f407 68013等使用

    原创OV7670,30W像素摄像头模块, 3) 光学尺寸1/6 ,像素面积3.6 μm x 3.6 μm,灵敏度1.3V/Lux-sec 4) 工作电压:3.3V 5) 接口定义为10*2的2.54间 ...

  3. 【Java Web开发学习】Spring JPA

    [Java Web开发学习]Spring JPA 转载:https://www.cnblogs.com/yangchongxing/p/10082864.html 1.使用容器管理类型的JPA JND ...

  4. Python异常体系结构图

  5. NLP(十八)利用ALBERT提升模型预测速度的一次尝试

    前沿   在文章NLP(十七)利用tensorflow-serving部署kashgari模型中,笔者介绍了如何利用tensorflow-serving部署来部署深度模型模型,在那篇文章中,笔者利用k ...

  6. orleans 2.0 进阶之自定义持久化储存

    一丶简单介绍下目录结构和项目依赖,如图 二丶主要核心自定义代码 1. 添加自定义实现类 CustomProvider public class CustomProvider : IGrainStora ...

  7. 关于ConfigurationSection自定义config的简单使用

    1.1.自定义config结构(参考对应颜色标注),放到configuration根节点下: <test> <testInfos> <" /> <& ...

  8. 使用CocoaPods配置iOS百度地图sdk问题记录20191024

    1.在Podfile中加入添加库名 pod 'BaiduMapKit' #百度地图SDK 2.安装百度地图 pod install 出现问题: [!] Error installing BaiduMa ...

  9. NGUI 源码分析- UIWidgetInspector

    NGUI Version 3.9.0 //---------------------------------------------- // NGUI: Next-Gen UI kit // Copy ...

  10. Angular 彻底解决 Dropdown 在 Safari 上无法自动关闭的问题

    之前在 Safari 上用 focus 事件来实现 Dropdown 下拉菜单,结果在 iOS 上不兼容. 尝试了 MDN 和 stack over flow 上各种奇技淫巧,然而在 iOS 上全都败 ...