Switch GameTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 15011 Accepted Submission (s): 9160
Problem Descriptionthere is many lamps in a line. All of them is off at first. A series of operations is carried out on these lamps. On the i-th operation, the lamps whose numbers is the multiple of I, the condition (on to off and off to on).
Inputeach test case contains only a number n (0< n<= 10^5) on a line.
Outputoutput the condition of the n-th lamp after infinity operations (0-off, 1-on).
Sample Input
15
Sample Output
HintHintConsider the second Test case:the initial condition : 0 0 0 0 0 ... After the first operation : 1 1 1 1 1 ... After the second Operation:1 0 1 0 1 ... After the third operation : 1 0 0 0 1 ... After the fourth Operation:1 0 0 1 1 ... After the fifth operation : 1 0 0 1 0 ... The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.
Authorll
Source Celebration Cup warm up
original title link: http://acm.hdu.edu.cn/showproblem.php?pid=2053
Test Instructions: There are n lights, the original is all closed, after the operation of N times, asked the last lamp state. Each time the number of lights is, toggle the light switch, (may be turned on or off).idea: The first thought is the simulation, but timed out, and later found that the state of N is determined by the approximate number of N, when about a number of even is, the state of N is unchanged (closed), when an odd number of times the change (open). So just count the number of numbers to solve the problem.
Small Discovery : When an approximate number is an odd number, the number must be squared! So this problem is changed to determine whether a number is a square number.principle:the approximate 30 is: (1,30), (2,15), (3,10) 36 of the approximate is: (1,36), (2,18), (3,12), (4,9), (6) an approximate number is always paired, and when the number of squares is equal to two, it is counted as one.
PS: The method of judging the square number is faster!
AC Code 1
#include <iostream> #include <cmath>using namespace Std;int main () { int n; while (Cin>>n) { double x=sqrt (n*1.0); cout<< (X==int (x)) <<endl; } return 0;}
AC Code 2:
#include <iostream>using namespace Std;int main () { int n; while (Cin>>n) { int sum=0; for (int i=1; i<=n; i++) { if (n%i==0) sum++; } cout<<sum%2<<endl; } return 0;}
HDU 2053 Switch Game (Turn on the light problem, the only decomposition theorem)