The Problem
There is man named "mabu" for switching on-off light in our University. He switches on-off the lights in a corridor. Every bulb has its own toggle switch. That is, if it is pressed then the bulb turns on. Another press will turn it off. To save power consumption (or may be he is mad or something else) he does a peculiar thing. If in a corridor there is `n' bulbs, he walks along the corridor back and forth `n' times and in i'th walk he toggles only the switches whose serial is divisable by i. He does not press any switch when coming back to his initial position. A i'th walk is defined as going down the corridor (while doing the peculiar thing) and coming back again.Now you have to determine what is the final condition of the last bulb. Is it on or off?
The Input
The input will be an integer indicating the n'th bulb in a corridor. Which is less then or equals 2^32-1. A zero indicates the end of input. You should not process this input.The Output
Output "yes" if the light is on otherwise "no" , in a single line.Sample Input
3 6241 8191 0
Sample Output
no yes no
题意 :有n盏灯,一开始全部关闭。然后从左往右走N趟,第 i 趟按一下序号为 i 的倍数的灯的开关。问走完之后,最后一盏灯是否开着。
一开始打算暴力,一看数据很大。放弃了。后来仔细一想,由于n如果能被k整除,那么n肯定也可以被(n/k)整除,所以是成对出现的。只有n是完全平方数,才会多出来一个。
第一次给wa了。主要是没有考虑到数据范围,int类型最大值是2^31-1..而unsigned类型才是2^32。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main ()
{
long long n,t;
while(cin>>n)
{
if (n==0) break;
t=sqrt(n*1.0);
if (t*t==n) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
灯泡切换谜题
本文探讨了一种特殊的灯泡切换问题,即一个走廊中有n盏灯,每盏灯都有一个开关,初始时所有灯都是关闭状态。有一个名为mabu的人会沿着走廊来回行走n次,每次行走时会切换特定编号灯的状态。文章最后通过数学方法给出了最后一盏灯最终状态的判断依据。
1176

被折叠的 条评论
为什么被折叠?



