PAT1078 Hashing 坑爹
来源:互联网 发布:黑米软件怎么安装 编辑:程序博客网 时间:2024/06/06 19:42
思路:用筛法给素数打表,二次探测法(只需要增加的)–如果
注意:如果输入的
AC代码
#include <stdio.h>#include <math.h>#include <string.h>const int maxn = 10000+20;int vis[maxn], pos[maxn];void getPrim(int n) { int m = sqrt(n+0.5); memset(vis, 0, sizeof(vis)); vis[0] = vis[1] = 1; for(int i = 2; i <= m; i++) if(!vis[i]) for(int j = i*i; j <= n; j += i) vis[j] = 1;}int main() { getPrim(maxn); int n, m; scanf("%d%d", &n, &m); if(vis[n]) { for(int i = n+1; i <= maxn; i++) { if(!vis[i]) { n = i; break; } } } //printf("%d\n", n); memset(pos, 0, sizeof(pos)); int key; for(int i = 0; i < m; i++) { scanf("%d", &key); int ok = 0; for(int j = 0; j < n; j++) { int hkey = (key+j*j) % n; if(!pos[hkey]) { ok = 1; pos[hkey] = 1; printf("%d%c", hkey, i == m-1 ? '\n' : ' '); break; } } if(!ok) printf("-%c", i == m-1 ? '\n' : ' '); } return 0;}
如有不当之处欢迎指出!
阅读全文
0 0
- PAT1078 Hashing 坑爹
- PAT1078 Hashing
- pat1078 Hashing
- PAT1078. Hashing (25)
- PAT1078 Hashing (25)
- pat1078 错题备份
- Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- 关于hashing
- Consistent hashing
- Cuckoo Hashing
- Consistent Hashing
- Consistent Hashing
- Extensible Hashing
- Flume + Solr + log4j搭建web日志采集系统
- 依赖注入那些事儿
- idea 查看方法注释时乱码
- iOS开发错误总结-持续更新
- 【异常】【Android Studio】Error:Failed to open zip file. Gradle's dependency cache may be corrupt
- PAT1078 Hashing 坑爹
- 剑指Offer:链表中环的入口结点
- python重点语法总结(三)----*和**的作用
- Delphi.NET 开发有哪些优势?
- Quartz调度器
- 智能指针
- Struts2 Annotation 注解配置
- 虚拟机下ubuntu与主机WinSCP配置
- Unity 动画知识之一