HDU 2053 Switch Game (Turn on the light problem, the only decomposition theorem)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.