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. sublime安装 less环境

    工具的选择: mac-codekit simpless->跨平台 winless-windows  less.js下载:http://pan.baidu.com/s/1o60yTZ0   安装L ...

  2. Ehcache(2.9.x) - Configuration Guide, Configuring Cache

    About Ehcache Configuration Ehcache supports declarative configuration via an XML configuration file ...

  3. Wireshark - 观察 ARP 地址解析过程

    下面使用 Wireshark 分析 ARP 的工作过程.试验的机器:发送者机器(IP 地址:10.21.28.47,MAC 地址:68:f7:28:0f:32:2e)下文称为 HOSTA:目标机器(I ...

  4. 对XML的操作

    对XML的操作主要使用到的语法示例: using System.Xml; private static string XmlMarketingStaff = AppDomain.CurrentDoma ...

  5. AIDL实现Android IPC

    1.AIDL文本解释 在软件工程中,接口定义语言(IDL)已经成为通用术语,是用来描述软件组件接口的特定语言.在Android中,该IDL被称为Android接口定义语言(AIDL),它是纯文本文件, ...

  6. SQL ser 跨实例访问数据库

    SqlServer如何跨实例访问数据库 Exec sp_droplinkedsrvlogin LinkName,NullExec sp_dropserver LinkName go EXEC sp_a ...

  7. Access数据库一种树形结构的实现和子节点查询

    BOOL CManageDataBase::GetDepTreeAllSons( int rootItem ) { CADORecordset Rst(&m_DataBase); BOOL b ...

  8. 上下问语句句柄Release地方

    OCI--在QUERY中 CLI--在FETCH中 在父类中定义了public—Release和protected—Release,protected—Release在public—Release中被 ...

  9. MySQL EER反向建表

    Database > Synchronize Model... Choose Stored Connection Select the Schemata Choose which to upda ...

  10. C#使用Zxing2.0生成二维码 带简单中心LOGO

    参考:http://www.open-open.com/lib/view/open1379214678162.html 代码:http://files.cnblogs.com/halo/%E4%BA% ...