码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2053 Switch Game(开灯问题,唯一分解定理)

时间:2016-05-13 00:54:34      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

Switch Game

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15011    Accepted Submission(s): 9160


Problem Description
There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).
 

Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.
 

Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).
 

Sample Input
1 5
 

Sample Output
1 0
Hint
hint
Consider 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.
 

Author
LL
 

Source


题意:有n盏灯,原来全部是关闭的,经过n次操作,问最后一盏灯的状态.每次次数是灯的编号是,拨动灯的开关,(有可能打开或者关闭).
思路:首先想到的是模拟,但是超时了,后来发现,n的状态由n的约数个数决定,当约数个数为偶数是,n的状态不变(关闭),当为奇数的时候发生改变(打开).所以只要统计约数的个数便可解决问题.

小发现:当约数个数为奇数的时候,这个数一定为平方数!所以此问题就转变为判断一个数是不是平方数.
原理:
30的约数为: (1,30), (2,15), (3,10)
36的约数为: (1,36), (2,18), (3,12), (4,9), (6)
一个数的约数总是成对的出现,当为平方数的时候有两个约数相同,就只算一个.

PS:判断平方数的方法速度更快!

AC代码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代码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(开灯问题,唯一分解定理)

标签:

原文地址:http://blog.csdn.net/hurmishine/article/details/51347080

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!