hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=4715
【code】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std;
#define N 1000151 int prim[N+];
int hash[];
int mark[];
int cjsb[];
int hash_cnt=;
int lowbit(int i)
{
return i&-i;
}
void add(int i,int a)
{
while(i<=N)
{
cjsb[i]+=a;
i+=lowbit(i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=cjsb[i];
i-=lowbit(i);
}
return s;
}
int main()
{
int i,j;
hash_cnt=;
for(i=;i<=N;i++)
{
if(!prim[i])
{
hash[hash_cnt++]=i;
for(j=;j*i<=N;j++)
{
prim[j*i]=;
}
}
}
// cout<<hash_cnt<<" "<<hash[hash_cnt-1]<<endl;
int tou=;
memset(cjsb,,sizeof(cjsb));
for(i=;i<hash_cnt;i++)
{
for(j=;j<=i-;j++)
{
int temp = hash[i]-hash[j];
if(temp<=tou)
break;
if(mark[temp]) continue;
else
{
mark[temp] = hash[i];
add(temp,);
if(sum(temp)==temp/)
tou=temp;
}
}
}
int n;
scanf("%d",&n);
while(n--)
{
int m;
scanf("%d",&m);
if(m==)
{
puts("2 2");
continue;
}
if(m<)
{
m=-m;
printf("%d %d\n",mark[m]-m,mark[m]);
}
else
{
printf("%d %d\n",mark[m],mark[m]-m);
}
}
return ;
}
hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)的更多相关文章
- hdoj 4715 Difference Between Primes 素数筛选+二分查找
#include <string.h> #include <stdio.h> const int maxn = 1000006; bool vis[1000006]; int ...
- HDU_3792_(素数筛+树状数组)
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- hdu 4715 Difference Between Primes
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army an ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)
题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路 ...
- HDU 5792:World is Exploding(树状数组求逆序对)
http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description Given a sequ ...
随机推荐
- css3 盒模型记
css3 盒模型 css假定每个元素都会生成一个或多个矩形框,这称为元素框.各元素框中心有一个内容区.这个内容区周围有可选的内边距,边框和外边距.这些项之所以被认为是可选的,原因是它们的宽度可以设置为 ...
- Java Concurrency - Semaphore 信号量
Semaphore 是一个控制访问多个共享资源的计数器. 当一个线程想要访问某个共享资源,首先,它必须获得 semaphore.如果 semaphore 的内部计数器的值大于 0,那么 semapho ...
- Linux 命令 - cd: 切换工作目录
命令格式 cd [directory] 实例 a) 将工作目录切换成主目录. cd # 或 cd ~ b) 将工作目录切换成当前工作目录的父目录. cd .. c) 将工作目录切换成上一个的工作目录. ...
- JavaScript设置cookie
在做网站的时候会用到JS操作cookie,现在写下来,算是对自己工作的一次小小总结,后面用到的时候就不用再写一遍了,高手就不用看了. /* 添加cookie 参数:cookie名,cookie值,过期 ...
- sql防注入式
SQL注入式攻击是利用是指利用设计上的漏洞,在目标服务器上运行Sql命令以及进行其他方式的攻击动态生成Sql命令时没有对用户输入的数据进行验证是Sql注入攻击得逞的主要原因.比如: 如果你的查询语句是 ...
- Contoso 大学 - 10 - 高级 EF 应用场景
原文 Contoso 大学 - 10 - 高级 EF 应用场景 By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Micros ...
- asp.net中C#对象与方法 属性详解
C#对象与方法 一.相关概念: 1.对象:现实世界中的实体 2. 类:具有相似属性和方法的对象的集合 3.面向对象程序设计的特点:封装 继承 多态 二.类的定义与语法 1.定义类: 修饰符 类名称 ...
- HTML5之图形变换
- Transformations scale(0.5,0.5) 缩放 rotate(0.175) 旋转 translate(100,50) 位移 - 代码结构 context.scale(x, y) ...
- java 枚举 类 enum
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...
- sbt的安装设置
文章转载自http://my.oschina.net/u/915967/blog/146746 本文主要是windows平台的安装,linux环境操作类似. 首先到http://www.scala-s ...