Bulb Switch


我觉得这题好扯啊。。。根本就测试不出面试者的水平。。。一个灯泡最后亮只有可能是因为开关被用了odd number times。 

第i 个灯泡开关会被处罚在第d轮只有当d divides i。 所以灯泡最后亮着只能是有一个odd number of divisors.

所以灯泡ends up on只有当i 是一个square。所以就是count the square numbers.

sqrt gives you the root of the largest square in the range [1,n]. And 1 is the smallest root. So you have the roots from 1 to sqrt, that's sqrt many roots. Which correspond to the sqrt many squares.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 年前的那个周末,女孩贝贝和妈妈一起逛花市,妈妈买了一盆风信子,贝贝说像蒜头一样的东东,一点不好看。妈妈说等到...
    安静的安阅读 249评论 0 0
  • 每一次回到爱人的老家都会很早醒来,今天也不例外,早上五点就醒了,听着窗外的公鸡打鸣声,并开始看书,书的名字叫做《你...
    Scott苏波毅阅读 183评论 0 0