当前位置: 首页 > news >正文

[转]个数为n的只读数组包含元素[1, n-1],求其中一个重复的数

题目网址:
http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/Challenges/January2004.html
解答网址:
http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/solutions/January2004.html

 

题目:
Ponder This Challenge:
This month's puzzle was sent in by Joe Buhler.
It came from a SIGCSE meeting via Eric Roberts.

A read-only array of length n, with address from 1 to n inclusive,
contains entries from the set {1, 2, ..., n-1}.
By Dirichlet's Pigeon-Hole Principle there are one or more duplicated
entries. Find a linear-time algorithm that prints a duplicated value,
using only "constant extra space". (This space restriction is important;
we have only a fixed number of usable read/write
memory locations, each capable of storing an integer between 1 and n.
The number of such locations is constant, independent of n.
The original array entries can not be altered.) The algorithm should
be easily implementable in any standard programming language.

 

解答:
Solution:

This technique is known as Pollard's rho method; it was developed by John Pollard as a tool for integer factorization.

Let f(x) be the location of the array at address x.

a := f(f(n))
b := f(n)
do while not (a=b)
    a:=f(f(a))
    b:=f(b)
end
/* Now a=b can be reached at either 2k or k steps from n, */
/* where k is some integer between 1 and n. */
a:=n
do while not (a=b)
    a:=f(a)
    b:=f(b)
end
print a

The pattern that you get, starting from n and repeatedly applying f (doing the table lookup), is a string of some length L>0 leading to a cycle of some length M, with L+M<n. (L is nonzero because n is outside the range of f.)

It is shaped like the Greek letter rho, hence its name. Our stopping count k is the least multiple of M greater than or equal to L, so that L \leq k < L+M < n.

 

更多材料:
Pollard's Rho Method

 

转载于:https://www.cnblogs.com/shokey520/p/3809467.html

相关文章:

  • 韩立刚老师51cto课程学习路线图
  • C#如何判断两个数组相等
  • 如何vs升级后10和12都能同时兼容
  • 应用wordpress
  • 使用 EF Power Tool Code Frist 生成 Mysql 实体
  • 【LeetCode】37. Sudoku Solver
  • 融合成就卓越F5诠释“软件定义”新内涵
  • mysql 用户管理和权限设置
  • C语言之内存分配例题详解
  • 收费版APP三年总结(个人经验)
  • [原创]2014年上半年测试书籍推荐
  • VCL 中的 Windows API 函数(6): BeginDeferWindowPos
  • 用T4 Template生成代码
  • YourSQLDba 配置——修改备份路径
  • Asp.NET MVC 技术参考:http://kb.cnblogs.com/zt/mvc/
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • HTTP中的ETag在移动客户端的应用
  • LintCode 31. partitionArray 数组划分
  • PHP 的 SAPI 是个什么东西
  • Spring Cloud中负载均衡器概览
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • WePY 在小程序性能调优上做出的探究
  • 初识MongoDB分片
  • 如何选择开源的机器学习框架?
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 用jQuery怎么做到前后端分离
  • - 语言经验 - 《c++的高性能内存管理库tcmalloc和jemalloc》
  • 自动记录MySQL慢查询快照脚本
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • 国内开源镜像站点
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • 如何正确理解,内页权重高于首页?
  • ​flutter 代码混淆
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • !!java web学习笔记(一到五)
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • $refs 、$nextTic、动态组件、name的使用
  • (3)nginx 配置(nginx.conf)
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (附源码)php新闻发布平台 毕业设计 141646
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (九)One-Wire总线-DS18B20
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • **PHP分步表单提交思路(分页表单提交)
  • . Flume面试题
  • .NET CF命令行调试器MDbg入门(一)
  • .NET 中什么样的类是可使用 await 异步等待的?