问题重述:

There are 100 doors in a long hallway. They are all closed. The first time you walk by each door, you open it. The second time around, you close every second door (since they are all opened). On the third pass you stop at every third door and open it if it’s closed, close it if it’s open. On the fourth pass, you take action on every fourth door. You repeat this pattern for 100 passes.

Question: At the end of 100 passes, what doors are opened and what doors are closed?

分析与解答:

这个问题没想通的觉得不难但是很繁,想通了觉得既不难也不繁。主要的想法就是说某编号的门如果被经过了奇数次,则门的状态与起始状态相反;如果被经过了偶数次,则门的状态与起始状态相同。

那么通用的做法是看该门编号对应的数能被多少数整除(除数要小于趟数)。如果除数个数是奇数则判定门的状态为起始状态的相反状态;否则,判定门的状态为起始状态。

这边,要不要这么做呢?wait……马克思爷爷曾经教导我们说:“具体问题具体分析!”。这个问题有什么具体情况呢?那就是:本题的趟数是和门数一致的。也就是说,只要看门的编号能被多少数整除就行了,不必担心需要除数小于等于趟数的限制条件。这样,我们就可以有一个条件了:那就是某个数的两个因子都在限制范围内。我们又发现,只要某个数能表示成两个因子的乘积,而这两个因子又互不相同,那么这两趟算是白跑啦,对门的状态没有影响。那么对门的状态有影响的,就是那两个因子相同的情况。这样,问题的解就是哪些编号为完全平方数的门啦!!!

自此,答案显而易见,状态有变的门的编号为:1,4,9,16,25,36,49,64,81,100。

参考文献:

1.        http://classic-puzzles.blogspot.com/2008/05/door-toggling-puzzle-or-100-doors.html

2.        http://www.theodorenguyen-cao.com/2008/02/02/puzzle-100-doors/

100 Door Puzzle的更多相关文章

  1. Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)

    题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广 ...

  2. POJ1651Multiplication Puzzle[区间DP]

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted:  ...

  3. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  4. Ignatius's puzzle

    Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. POJ1651Multiplication Puzzle(矩阵链乘变形)

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8040   Accepted: ...

  6. POJ1651:Multiplication Puzzle(区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...

  7. HDOJ 1098 Ignatius's puzzle

    Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...

  8. UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>

    F - Eight Puzzle Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) ...

  9. codeforces A. Orchestra B. Island Puzzle

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. Percona Toolkit 使用

    安装 percona-toolkit perl Makefile.PL make make test make install 默认安装到 /usr/local/bin 目录下 可能需要 DBI-1. ...

  2. Azure

    ylbtech-Miscellaneos:Azure A,返回顶部 1, Windows Azure是微软基于云计算的操作系统,现在更名为“Microsoft Azure”,和Azure Servic ...

  3. CentOS7:Puppet推送Zabbix Agent

    创建zabbix模块目录: $ mkdir -p /etc/puppet/modules/zabbix/{manifests,templates} 创建init.pp清单: $ cat /etc/pu ...

  4. SharePoint 2013 SP1发布了

    好消息,SharePoint 2013 SP1终于发布了: SP1说明:http://support.microsoft.com/kb/2880552 下载页面:http://www.microsof ...

  5. Linux学习笔记之——基础命令学习

    1.find 按照名字查找:find / -name file_name   2.zip压缩 1) 我想把一个文件repartition.txt和一个目录invader压缩成为amateur.zip: ...

  6. inux grep 命令 搜索含有"zynq"字符的文件

    使用命令grep -rl 'zynq' /work/xilinx/u-boot-xlnx-master (有引号)-r 选项表示递归(recursive)遍历所有子目录-l 选项表示只列出文件名 /w ...

  7. 对想进入Unity开发新人的一些建议

    提前声明:本文只是写给那些非职业游戏开发人士,只面向那些在校本科生,或已就业但无unity背景的同学们,当然是面对程序员方向的.本人刚工作也没多久,资历尚浅,之前在网上有一位同学让我谈谈一些想法,所以 ...

  8. Linux 权限设置

    一.文件和目录权限 在Linux系统中,用户可以对每一个文件或目录都具有访问权限,这些访问权限决定了谁能访问,以及如何访问这些文件和目录. 1.文件权限简介 在Linux系统中,每一位用户都有对文件或 ...

  9. BCM94352HMB蓝牙BCM20702A0在Ubuntu 14.04下的驱动方法

    作者:秋忆 出处:http://www.cnblogs.com/qiuyi21/p/bcm20702a0_ubuntu.html 先确认蓝牙的产商编号(idVendor)和产品编号(idProduct ...

  10. linux ssh publickey登录

    一.公钥认证的基本思想: 对信息的加密和解密采用不同的key,这对key分别称作private key和public key,其中,public key存放在目标服务器上,而private key为特 ...