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 ...
随机推荐
- Microsoft.SharePoint.Security的问题
请求“Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0 ...
- HttpClient(4.3.5) - HTTP Header
An HTTP message can contain a number of headers describing properties of the message such as the con ...
- jBPM - jBPM Installer
Prerequisites This script assumes you have Java JDK 1.6+ (set as JAVA_HOME), and Ant 1.7+ installed. ...
- C#数组比较取值
string strs = string.Empty; string[] strArray1 = { "a", "b", "c& ...
- 配置对IIS上tabular的 HTTP 访问
上周网管说某部门一同事用的自家电脑办公,操作系统是正版win8家庭版,不能加入公司域,求解如何访问数据仓库. 以前一直以为只有域内用户才能使用数据仓库,没办法有问题总要给人解决,一味地推脱不但会影响其 ...
- Win32非递归遍历和搜索文件以及目录算法
转载请注明来源:http://www.cnblogs.com/xuesongshu 要点: 1.搜索的顶层目录在进入循环之前进栈 2.栈元素存储字符串指针,出栈时释放资源 3.每次循环开始,栈顶元素出 ...
- JAR、WAR、EAR 区别
JAR包 JAR 文件格式以流行的 ZIP 文件格式为基础: 与 ZIP 文件不同的是,JAR 文件不仅用于压缩和发布,而且还用于部署和封装库.组件和插件程序,并可被像编译器和 JVM 这样的工具直接 ...
- jquery的map()和each()方法
1. map()方法 //找到所有的标题元素,映射它们的ID,并转化为数组后排序 $(':header').map(function(){return this.id}).toArray().sort ...
- MVC3 ViewBage 输出的值 被编码
问题描述: 后台,Actoin中我向ViewBage中加入了一个json ViewBage.JsonDateMenu ="[{\"id\":2,\" ...
- 【C#高级编程(学习与理解)】1.1 C#与.NET的关系
1.C#语言不能孤立使用,而必须和.NET Framework一起考虑.C#编译器专门用于.NET,这表示用C#编写的所有代码总是在.NET Framework中运行. 2.C#就其本身而言只是一种语 ...