传送门
Pssword: nefu


DescriptionAmakusa, 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!InputInput 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).OutputFor each case, print the case number and the number of safe territories.Sample Input32 363 733 11Sample OutputCase 1: 11Case 2: 20Case 3: 4

题目大意:
就是给定一个区间 让你求这个区间有多少个素数,数据范围(1 ≤ a ≤ b < 2^31, b - a ≤ 100000).

解题思路:
首先看到数据范围还是比较大的, 但是他们之间的区间差很小,所以我们就考虑用区间差值计算,因为我们不可能开一个 2^31次方的数组,根本开不了那么大,所以我们采用的方法就是筛法,首先进行第一次素数筛,将 1—1e6之间的素数筛出来,筛完之后我们要做的是在[0,b-a]区间进行筛,怎么筛呢,我就详细的说一下啦:
首先进行 for 循环,从0—b-a,而且得保证p[i]*p[i]<=b(p[i]是存的素数值)让 a 对每一个p[i]值进行取余,我们要筛的就是(a+(p[i]-a%p[i]))%p[i] == 0的数,然后就跟素数筛差不多啦…具体还得看我的代码 代码还是挺好理解的 ^_^
上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;#define MM(a) memset(a,0,sizeof(a))typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = 1e6+5;
const int mod = 1000000007;
const double eps = 1e-7;
bool prime[MAXN];
LL p[MAXN];
LL k;
void isprime()///素数筛
{k = 0;prime[1] = false;memset(prime, true, sizeof(prime));for(LL i=2; i<MAXN; i++){if(prime[i]){p[k++] = i;for(LL j=i*i; j<MAXN; j+=i)prime[j] = false;}}
}
LL a, b, tmp;
bool ok[MAXN];
void ShaiXuan()
{memset(ok, true, sizeof(ok));tmp = b - a;for(LL i=0; p[i]*p[i]<=b&&i<k; i++){LL tt = 0;if(a%p[i])///第一个筛掉的数是(tt+a)%p[i] == 0tt = p[i] - a%p[i];if(a <= p[i])///防止是素数tt += p[i];for(; tt<=tmp; tt+=p[i])///筛掉p[i]的倍数ok[tt] = 0;}
}
int main()
{isprime();int T;cin>>T;for(int cas=1; cas<=T; cas++){cin>>a>>b;ShaiXuan();LL ret = 0;if(a == 1)ret = -1;for(int i=0; i<=tmp; i++)if(ok[i])ret++;printf("Case %d: %lld\n",cas,ret);}return 0;
}

E - Help Hanzo(LightOJ 1197)相关推荐

  1. Help Hanzo(LightOJ - 1197)(欧拉筛 + 思维)

    LightOJ - 1197 Help Hanzo 来源:LightOJ - 1197 Help Hanzo 题意: 判断a ~ b区间内,有多少个素数,a,b范围[1, 2 ^ 31 - 1],b ...

  2. Help Hanzo LightOJ - 1197(素数筛法)

    Help Hanzo LightOJ - 1197 题目链接: https://cn.vjudge.net/problem/26909/origin 题意: 求两个数a,b之间有多少个素数. 思路: ...

  3. 【代码超详解】LightOJ 1197 Help Hanzo(区间质数筛法)

    一.题目描述 二.算法分析说明与代码编写指导 对于求指定区间 [a, b] 的质数的题目,通常 a 和 b 都比较大,而 b - a 不太大. 采用埃氏筛或者欧拉筛的代码,一般都会同时给出前若干个质数 ...

  4. LightOJ 1197 Help Hanzo

    LightOJ 1197 Help Hanzo 题目链接 Amakusa, the evil spiritual leader has captured the beautiful princess ...

  5. Birthday Paradox(生日悖论)(概率)

    Birthday Paradox(生日悖论)(概率) judge:LightOJ - 1104 vjudge:vjudge Time limit:2000 ms Memory limit:32768 ...

  6. OJ链接(持续更新)

    UVaOJ    http://uva.onlinejudge.org 西班牙Valladolid大学的法度在线评测体系,是汗青最悠长.最有名的OJ. USACO   http://www.nocow ...

  7. 无人驾驶数据库汇总(不断更新)

    文章目录 1.0自动驾驶数据集 1.1 KITTI 数据库 1.1.1传感器 1.1.2坐标系 1.1.3 时间戳 1.1.4 同步 1.1.5相机校准 1.1.6图像 1.1.7投影计算 1.1.8 ...

  8. 128Echarts - 关系图(NPM Dependencies)

    效果图 源代码 <!DOCTYPE html> <html><head><meta charset="utf-8"><titl ...

  9. 李宏毅(2020)作业10:异常检测(Anomaly Detection)

    文章目录 数据集 目标 数据 作业 KNN PCA Autoencoder 评估 数据集 数据集下载test 数据集下载train 目标 Semi-supervised anomaly detecti ...

最新文章

  1. java post xmll_HttpClient发送Post请求,内容格式为xml,并获取响应内容
  2. xmanager破解待验证
  3. cbc cryptojs 前后端_前端CryptoJS AES/DES加解密与后端PHP AES/DES加解密
  4. 直方图 帕累托图_如何发现现象背后的关键因素?帕累托图,质量管理的利器...
  5. 简单理解极大似然估计MLE
  6. LEADTOOLS Multimedia SDK更新:改进RTSP和H.265/H.264的硬件加速
  7. linux下overcommit_memory的问题
  8. Ros_Topic通信方式
  9. 区块链项目开发指南大纲
  10. 二维数组求最小值_求一列中满足条件的最大最小值
  11. diff命令两个服务器文件,LINUX命令diff-文件管理-比较给定的两个文件的不同
  12. 因为此网站使用了 hsts_HSTS原理及实践
  13. 用 PHP 来玩直播猜题小游戏,一起 happy coding.
  14. weblogic 启动 startWebLogic.sh
  15. 存储器分类和硬盘缓存介绍
  16. EXCEL取消科学计数法
  17. 实习测试的一个月总结与心得
  18. 微软的SqlHelper做数据层(一)
  19. zigbee抓包时为何时常出现这个not able to setup connection to device smartRF04EB
  20. echarts瀑布图_一种基于阶梯瀑布图的数据计算方法与流程

热门文章

  1. 若能坚定信念,就能开创美好的未来
  2. # 20155327 2016-2017-4 《Java程序设计》第七周学习总结
  3. dms虚拟服务器,iOS UPNP之DMS具体操作,让手机成为DMS服务器
  4. 如何区分电梯卡为id卡ic卡_电梯ic卡系统与ID卡系统有什么区别
  5. 【Linux】Linux 磁盘与文件系统管理命令
  6. 旅客因航班耽搁殴打工作职员被拘
  7. SICTF2023 Osint-wp
  8. 全球及中国铁矿石行业供求状况与投资决策建议报告2022版
  9. 打印机不能正常打印怎么办
  10. mvc2 mvc_迅捷的MVC