Help Hanzo (素数筛+区间枚举)
题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000). (全题在文末)
题解:
a~b枚举必定TLE,普通打表MLE,真是头疼。。
b - a ≤ 100000 是关键。
类似素数筛的方法:
1.初始化vis[]=0 ;
2.素数的倍数vis[]=1;
3. b较小时,素数筛解决
b很大时,素数筛的vis[]会MLE,此时用vis2[i-a]保存vis[i]就不会MLE 了。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1e5+; bool vis[N],visab[N];
int prime[N],cnt=;
void is_prime()
{
memset(vis,,sizeof(vis));
vis[]=;
for(int i=;i<N;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
for(int j=i+i;j<N;j+=i)
vis[j]=;
}
}
} int main()
{
int t;
cin>>t;
is_prime();
for(int kase=;kase<=t;kase++)
{
LL a,b;
scanf("%lld%lld",&a,&b);
int count=;
if(b<=N-)
{
for(LL i=a;i<=b;i++)
{
if(!vis[i])
count++;
}
}
else
{
memset(visab,,sizeof(visab));
for(int i=;i<cnt&&prime[i]<=b;i++)
{
LL k=a/prime[i];
if(k*prime[i]<a)
k++;
for(LL j=k*prime[i];j<=b;j+=prime[i])
{
visab[j-a]=;
}
}
for(LL i=a;i<=b;i++)
{
if(!visab[i-a])
count++;
}
}
printf("Case %d: %d\n",kase,count);
}
}
Help Hanzo
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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
Help Hanzo (素数筛+区间枚举)的更多相关文章
- Light oj 1197 - Help Hanzo (素数筛技巧)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法 ...
- LightOJ 1197 Help Hanzo 素数筛
题意:筛一段区间内素数的个数,区间宽度10w,区间范围INT_MAX 分析:用sqrt(INT_MAX筛一遍即可),注意先筛下界,再筛上届,因为有可能包含 #include <cstdio> ...
- [hdu 6069]素数筛+区间质因数分解
给[L,R]区间的每一个数都质因数分解的复杂度可以达到(R-L)logR,真的涨姿势…… 另外,质因数分解有很重要的一点,就是只需要打sqrt(R)以内的素数表就够了……因为超过sqrt(R)的至多只 ...
- LightOj 1197 Help Hanzo 区间素数筛
题意: 给定一个区间a,b,a-b>=100000,1<=a<=b<=231,求出给定a,b区间内的素数的个数 区间素数筛 (a+i-1)/ ii向上取整,当a为 i 的整数倍 ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- BestCoder Round #85 hdu5778 abs(素数筛+暴力)
abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...
- [luoguP1835] 素数密度_NOI导刊2011提高(04)(素数筛)
传送门 数据辣么大,怎么搞?(L≤R≤2147483647) 注意到R-L≤1000000 所以可以直接筛R-L区间内的数, 但是需要用已知的小的素数筛, R-L区间内的大部分数肯定能用较小的素数筛去 ...
- Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- Codeforces J. Soldier and Number Game(素数筛)
题目描述: Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- 【.net 深呼吸】细说CodeDom(7):索引器
在开始正题之前,先补充一点前面的内容. 在方法中,如果要引用方法参数,前面的示例中,老周使用的是 CodeVariableReferenceExpression 类,它用于引用变量,也适用于引用方法参 ...
- NodeJs之Path
Path模块 NodeJs提供的Path模块,使得我们可以对文件路径进行简单的操作. API var path = require('path'); var path_str = '\\Users\\ ...
- 构建通用的 React 和 Node 应用
这是一篇非常优秀的 React 教程,这篇文章对 React 组件.React Router 以及 Node 做了很好的梳理.我是 9 月份读的该文章,当时跟着教程做了一遍,收获很大.但是由于时间原因 ...
- vue.js学习笔记
有了孩子之后,元旦就哪也去不了了(孩子太小),刚好利用一些时间,来公司充充电补补课,学习学习新技术,在这里做一个整理和总结.(选择的东西,既然热爱就把他做好吧!). 下来进入咱们的学习环节: 一.从H ...
- notepad++设置默认打开txt文件失效的解决方法
1.系统环境 win10企业版,64位系统 2.初步设置 设置txt默认为notepad++打开,菜单:设置->首选项->文件关联 选择对应的文件扩展,点击"关闭"按钮 ...
- 去IOE的一点反对意见以及其他
某天在机场听见两老板在聊天,说到他们目前销售的报表老跟不上的问题,说要请一个人,专门合并和分析一些发过来的excel表格,我真想冲上去说,老板,你需要的是一个信息处理的系统,你需要咨询么.回来一直耿耿 ...
- BPM的魅力何在?
BPM(Business Process Management , 企业流程管理平台) 是带动企业流程自动化的帮 手,也是最能忠实反应出企业作业流程问题症结的系统工具,在管理上,BPM可以让管理者利用 ...
- Eclipse使用Git教程
A:点击Window--->Show view--->other..--->Git Repositories--->[OK] B:克隆码云上的代码仓库 C:选择对应目录存储你的 ...
- MySQL 数据库双向同步复制
MySQL 复制问题的最后一篇,关于双向同步复制架构设计的一些设计要点与制约. 问题和制约 数据库的双主双写并双向同步场景,主要考虑数据完整性.一致性和避免冲突.对于同一个库,同一张表,同一个记录中的 ...
- .NET面试题系列[3] - C# 基础知识(1)
1 类型基础 面试出现频率:基本上肯定出现 重要程度:10/10,身家性命般重要.通常这也是各种招聘工作的第一个要求,即“熟悉C#”的一部分.连这部分都不清楚的人,可以说根本不知道自己每天都在干什么. ...