UVA10140PrimeDistance题解--质数/技巧
题目链接
https://www.luogu.org/problemnew/show/UVA10140
分析
\(L,R\)都很大,显然不能直接筛出\(L,R\)区间中的质数,这里需要一个结论
结论
任何一个合数\(N\)必定含有一个小于等于\(\sqrt N\)的质因子
证明
反证法,若所有质因子都大于\(\sqrt N\),那么无论怎么组合显然都大于\(N\)
于是通过这个结论筛出\([2,\sqrt R]\),中的所有素数,把它们看作质因子筛出\([L,R]\)中的所有合数
注意如果\(L\)为\(1\)的话需要注意,不要把\(1\)标记为质数,对拍了好久才发现...真坑啊
代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <queue>
#include <cmath>
#define ll long long
#define ri register int
using std::min;
using std::max;
template <class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;return ;
}
const int maxn=1500005;
const int inf=0x7fffffff;
int l,r;
bool ok[maxn],vis[50000];
int prime[50000],tot=0;
inline void get_prime(){
int lim=(int)(std::sqrt(1.0*inf+0.5));
//printf("%d\n",lim);
for(ri i=2;i<=lim;i++){
if(!vis[i]){
vis[i]=1;
prime[++tot]=i;
}
for(ri j=1;j<=tot&&1ll*prime[j]*i<=lim;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
return ;
}
inline void solve(int n){
int lim=(int)(std::sqrt(1.0*n+0.5));
memset(ok,0,sizeof(ok));
int o=1;
while(prime[o]<=lim&&o<=tot){
for(ri k=l/prime[o];k<=r/prime[o];k++){
if(k==1||k==0)continue;
/*注意k=1及k=0都是不合法的情况*/
ok[k*prime[o]-l]=1;
/*左移L位以储存结果*/
}
o++;
}
return ;
}
int main(){
freopen("dat.in","r",stdin);
freopen("wa.out","w",stdout);
get_prime();
while(scanf("%d %d",&l,&r)!=EOF){
/*注意!!!*/
if(l==1)l=2;
//注意!!! 1不是质数
int lnum,lst=-inf,xans=-1,ians=inf;
int a,b,c,d;
solve(r);
for(ri i=0;i<=r-l;i++){
if(!ok[i]){
if(lst!=-inf){
//xans=max(xans,i-lst);
if(i-lst>xans){
c=lst,d=i;
xans=i-lst;
}
if(i-lst<ians){
a=lst,b=i;
ians=i-lst;
}
//ians=min(ians,i-lst);
}
lst=i;
}
}
if(xans==-1){
puts("There are no adjacent primes.");
}
else{
printf("%d,%d are closest, %d,%d are most distant.\n",a+l,b+l,c+l,d+l);
}
}
return 0;
}
UVA10140PrimeDistance题解--质数/技巧的更多相关文章
- HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解
本题又是一题单调队列题解. 技巧就是须要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tai ...
- ACdream 1195 Sudoku Checker (数独)
Sudoku Checker Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
- [LeetCode] 196.删除重复的电子邮箱
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +-- ...
- arc098E Range Minimum Queries
题意:给你一个n个数的数组,每次能够选取连续的长度为K的子序列,取出其中任意一个最小元素. 一共操作Q次.问取出的元素中Max-Min最小是多少? 标程: #include<bits/stdc+ ...
- soj114 中位数
题意:给你一个长度为2n-1的数组A,设Bi是A的1~2i-1的中位数.问打乱A,有多少种不同的B序列? 标程: #include<bits/stdc++.h> using namespa ...
- [USACO08OPEN]牛的街区Cow Neighborhoods
题目描述: luogu 题解: 技巧题. 曼哈顿距离:$|x1-x2|+|y1-y2|$ 切比雪夫距离:$\max(|x1-x2|,|y1-y2|)$ 曼哈顿距离转切比雪夫距离:$(x,y)-> ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(技巧+暴力枚举+线性筛)
技巧:就是偶数位的回文数字一定不是质数---------证明:奇数位之和sum1==偶数位之和sum2的数字可以被11整除.(11除外,这是一个坑点) 最高位,最低位必须是 1, 3, 7, 9 暴力 ...
- 【题解】HDU4689 Derangement(有技巧的计数DP)
[题解]HDU4689 Derangement(有技巧的计数DP) 传送门 呵呵没告诉我多测组数,然后\(n\le 20,7000\mathrm{ms}\)我写了个状压上去T了 题目大意: 要你求错排 ...
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
随机推荐
- HearthAgent A Hearthstone agent
http://www.intelligence.tuc.gr/~robots/ARCHIVE/2015w/Projects/LAB51326833/download.html The project ...
- Jmeter-app接口
1.IOS登录接口涉及的三个接口: 2.三个接口传入的参,第三个是判断用户是否登录成功的 http://118.178.247.67:8449/service/userLogin/phoneQuick ...
- JMeter-正则表达式(取出银行卡号后4位)
{ : "custName":"奚红艳", : "banks": : [ : : { : : : "id":" ...
- embeddable persistent key-value store for fast storage
A persistent key-value store for fast storage environmentsRocksDB is an embeddable persistent key-va ...
- 使用浏览器连接Linux服务器
wssh 可以让我们通过 HTTP 来调用远程的一个 shell,也就是说我们可以用浏览器来访问某个 Linux 服务器/虚拟机的终端(只要这个服务器上运行了 wsshd 服务器端).wssh 客户端 ...
- ios 本地模糊搜索
/* 手机 选择区域 模型 */ #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface MYSelect ...
- OpenStack Cinder发展动态系列--Austin峰会
在Mitaka版本,Cinder团队在多个特性和领域取得了重大进展. 本文将做一个简要的介绍:关于在Mitaka版本已经完成的功能和特性,以及讨论在Newton版本将会开发的功能和特性. 1 Cind ...
- Spring事务管理3----声明式事务管理(1)
声明式事务管理(1)基于 基于 tx/aop 这种事务管理相比编程式事务管理来说对业务层基本没有改动,通过 TransactionProxyFactoryBean 创建业务层的代理,通过A ...
- 动手生成 Delphi xe DBTreeview 三级行政图树 省市县
表结构及记录 object tarea: TFDQuery Connection = FDConnection1 SQL.Strings = ( 'select *' ...
- 【leetcode_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...