E - Help Hanzo(LightOJ 1197)

简介:

传送门
Pssword: nefu


Description


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

题目大意:
就是给定一个区间 让你求这个区间有多少个素数,数据范围(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] == 0
            tt = 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;
}
目录
相关文章
|
异构计算
实验三 基于FPGA的数码管动态扫描电路设计 quartus/数码管/电路模块设计(下)
实验三 基于FPGA的数码管动态扫描电路设计 quartus/数码管/电路模块设计(下)
684 0
实验三 基于FPGA的数码管动态扫描电路设计 quartus/数码管/电路模块设计(下)
|
安全 应用服务中间件 Windows
关于将Web项目部署到阿里云服务器-5个步骤搞定
一、 先登录阿里云网站注册账号 选择服务器类型(我用的是 云服务器ECS), 如果你还是在读大学生可享受优惠价,最低好像是9.9元一月。之后勾选系统镜像。 二、 购买好之后登录阿里云控制台。 https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=2yfpwghd也可以在Windows本机端下的 远程桌面连接 如下图, 步骤 : 1.找到开始菜单下远程桌面连接 2. 输入 公网ip地址 3. 输入用户名:Administrator 密码:就是登录window系统的密码 三、连接成功如下图。
13764 2
|
1天前
|
存储 关系型数据库 MySQL
数据管理的艺术:PolarDB开源版详评与实战部署策略(一)
PolarDB-X是阿里巴巴自研的高性能云原生分布式数据库,基于共享存储的Shared-nothing架构,支持MySQL生态,具备金融级高可用、分布式水平扩展、HTAP混合负载等能力。它通过CN(计算节点)和DN(存储节点)实现计算与存储分离,保证数据强一致性,并支持全局二级索引和多主多写。PolarDB-X开源版提供更高程度的定制化和控制权,适合追求技术自主性和成本优化的开发者。部署方式包括RPM包、PXD工具和Kubernetes,其中PXD工具提供了一键部署的便利性。
38269 10
|
5天前
|
关系型数据库 Serverless 分布式数据库
高峰无忧,探索PolarDB PG版Serverless的弹性魅力
在数字经济时代,数据库成为企业命脉,面对爆炸式增长的数据,企业面临管理挑战。云原生和Serverless技术革新数据库领域,PolarDB PG Serverless作为阿里云的云原生数据库解决方案,融合Serverless与PostgreSQL,实现自动弹性扩展,按需计费,降低运维成本。它通过计算与存储分离技术,提供高可用性、灾备策略和简化运维。PolarDB PG Serverless智能应变业务峰值,实时监控与调整资源,确保性能稳定。通过免费体验,用户可观察其弹性性能和价格力,感受技术优势。
|
15天前
|
存储 缓存 监控
你的Redis真的变慢了吗?性能优化如何做
本文先讲述了Redis变慢的判别方法,后面讲述了如何提升性能。
102247 5
|
15天前
|
机器学习/深度学习 并行计算 算法
Transformer 一起动手编码学原理
学习Transformer,快来跟着作者动手写一个。
94255 8
|
14天前
|
存储 SQL Apache
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
|
19天前
|
人工智能 弹性计算 算法
一文解读:阿里云AI基础设施的演进与挑战
对于如何更好地释放云上性能助力AIGC应用创新?“阿里云弹性计算为云上客户提供了ECS GPU DeepGPU增强工具包,帮助用户在云上高效地构建AI训练和AI推理基础设施,从而提高算力利用效率。”李鹏介绍到。目前,阿里云ECS DeepGPU已经帮助众多客户实现性能的大幅提升。其中,LLM微调训练场景下性能最高可提升80%,Stable Difussion推理场景下性能最高可提升60%。
124030 50
|
15天前
|
存储 弹性计算 Cloud Native
1 名工程师轻松管理 20 个工作流,创业企业用 Serverless 让数据处理流程提效
为应对挑战,语势科技采用云工作流CloudFlow和函数计算FC,实现数据处理流程的高效管理与弹性伸缩,提升整体研发效能。
64755 2
|
21天前
|
消息中间件 安全 API
Apache RocketMQ ACL 2.0 全新升级
RocketMQ ACL 2.0 不管是在模型设计、可扩展性方面,还是安全性和性能方面都进行了全新的升级。旨在能够为用户提供精细化的访问控制,同时,简化权限的配置流程。欢迎大家尝试体验新版本,并应用在生产环境中。
187602 38