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(素数筛选+树状数组哈希剪枝)的更多相关文章

  1. hdoj 4715 Difference Between Primes 素数筛选+二分查找

    #include <string.h> #include <stdio.h> const int maxn = 1000006; bool vis[1000006]; int ...

  2. HDU_3792_(素数筛+树状数组)

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  4. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

  5. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  6. 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 ...

  7. 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 ...

  8. Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)

    题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路 ...

  9. HDU 5792:World is Exploding(树状数组求逆序对)

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description   Given a sequ ...

随机推荐

  1. 直接查数据导出xls

    直接查数据导出xls/usr/bin/mysql --default-character-set=utf8 -uroot -p"123456" -e"select * f ...

  2. web-app1--移动端等比例代码

    1  :   <meat name="viewport" content="width=device-width,initial-scale=1"/> ...

  3. namenode无法启动(namenode格式化失败)

    格式化namenode root@node04 bin]# sudo -u hdfs hdfs namenode –format 16/11/14 10:56:51 INFO namenode.Nam ...

  4. android结束进程、退出application的方法

    1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...

  5. 第十篇、Swift -- WebSocket

    每当小编再开发中遇到了困难,在网上搜,简直是垃圾堆里找金子.国内网站真的全不可靠,最后FQ去国外网站寻找,才可以找到.找到了写websocket文章,同时找到了集成的框架文件,一个叫Starscrea ...

  6. iOS UIView 快速修改 frame

    我们修改frame中的某个值,需要进行繁琐的书写,例如: (1). 直接设置位置大小 view.frame = CGRectMake(0, 0, 320, 150); (2). 只修改某个值 view ...

  7. JavaScript学习笔记(12)——JavaScript内置对象

    1.Number Javascript只有一种数字类型,可以有小数也可以没有,也可以使用科学计数法. var z=123e-5; // 0.00123 JavaScript 不是类型语言.与许多其他编 ...

  8. 【译】 Node.js v0.12的新特性 -- 性能优化

    原文: https://strongloop.com/strongblog/performance-node-js-v-0-12-whats-new/ January 21, 2014/in Comm ...

  9. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  10. ngx_cdecl

    ngx_cdecl 作为跨平台用,现在理解有限,以后补充 _cdecl 是C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些 ...