时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -
 #include<stdio.h>
#include<math.h> int main()
{
int i,MSize,n,j;
bool isprime[];
bool isexit[];
isprime[] = false;
for(i= ;i<=;i++)
{
bool is = true;
for(j=;j<=sqrt(i*1.0);j++)
{
if(i % j == )
{
is = false;
break;
}
} isprime[i] = is;
//if(is) printf("%d ",i);
}
while(scanf("%d%d",&MSize,&n)!=EOF)
{
while(isprime[MSize]==false)
++MSize; for(i = ;i<MSize ;i++)
isexit[i] = false; bool fir =true;
for(i = ;i<n;i++)
{
int tem;
scanf("%d",&tem);
if(isexit[tem%MSize]==false)
{
if(fir)
{
printf("%d",tem%MSize);
fir = false;
}
else
{
printf(" %d",tem%MSize);
}
isexit[tem%MSize] = true;
}
else
{
bool find = false;
for(j = ;j <MSize;j++)
{
if(isexit[(tem+j*j)%MSize]==false)
{
find = true;
printf(" %d",(tem+j*j)%MSize);
isexit[(tem+j*j)%MSize] = true;
break;
}
} if(!find) printf(" -");
}
} printf("\n");
}
return ;
}

1078. Hashing (25)的更多相关文章

  1. 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise

    题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...

  2. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  3. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  4. PAT甲题题解-1078. Hashing (25)-hash散列

    二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...

  5. PAT (Advanced Level) 1078. Hashing (25)

    二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...

  6. 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)

    题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...

  7. PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]

    题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...

  8. 1078 Hashing (25分)

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  9. pat1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

随机推荐

  1. poj2299解题报告(归并排序求逆序数)

    POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...

  2. css笔记02:选择器(标签式和类)

    body { margin:; padding:; background:#000 url('images/backgrounds/star.png') no-repeat fixed; font: ...

  3. 树形菜单 jsTree 使用方法

    jsTree版本:3.0.4 在ASP.NET MVC中使用jsTree Model: public class Department { public int Id { get; set; } pu ...

  4. 关于线程池ThreadPoolExecutor使用总结

    本文引用自: http://blog.chinaunix.net/uid-20577907-id-3519578.html 一.简介 线程池类为 java.util.concurrent.Thread ...

  5. mysql数据库千万级别数据的查询优化和分页测试

    原文地址:原创 mysql数据库千万级别数据的查询优化和分页测试作者:于堡舰 本文为本人最近利用几个小时才分析总结出的原创文章,希望大家转载,但是要注明出处 http://blog.sina.com. ...

  6. 实用工具推荐(Live Writer)(2015年05月26日)

    1.写博客的实用工具 推荐软件:Live Writer 使用步骤: 1.安装 Live Essential 2011,下载地址:http://explore.live.com/windows-live ...

  7. jQuery中的经典动画

    show()方法和hide()方法是jQuery中的基本动画方法,hide()方法等于将css()方法设置display属性为none.如何让元素动起来呢,我们可以在show和hide里加入slow, ...

  8. Unity3D 之UGUI 滚动条

    先上效果图. 这里来说明下UGUI 滚动条,不涉及到代码. 主要用到的控件Scroll Rect ,Mask,Scrollbar. 第一步,建立一个Image,然后绑定一个滑动块的组件,添加一个mas ...

  9. 浅谈.NET中加密和解密的实现方法分享

    这篇文章介绍了.NET中加密和解密的实现方法,有需要的朋友可以参考一下 .NET将原来独立的API和SDK合并到一个框架中,这对于程序开发人员非常有利.它将CryptoAPI改编进.NET的Syste ...

  10. Java -- Thread中start和run方法的区别

    一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...