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
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 fif
th operation : 1 0 0 1 0 … The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.
分析:
1.本题的意思是进行无穷次开关之后,输出第几盏灯的亮暗状态;只有在是i的倍数,才switch一次。偶数次的swtich是不变的0,奇数次才变化为1;
因此只需要计算给定n中因子的奇偶性就可以了,代码1可以AC。
2.再思考下,其实会发现乘积因子都是一对对的,如16=2*8=1*16=4*4 ,其实只要不是完全平方数,那因子总数一定是偶数,只有完全平方数才是奇数,这样只要判断是否完全平方数既可,很简单。
#include <iostream> #include <stdlib.h> using namespace std; int main() { int n,i; int count; while(scanf("%d",&n)!=EOF) { count = 0; for(i=1;i<=n;i++) if(n%i==0) count++; if(count & 0x01) printf("1\n"); else printf("0\n"); /* printf("%d\n",count &0x01);//判断奇数为1 */ } return 0; }
printf("%d\n",((int)sqrt((double)n)*(int)sqrt((double)n))==n);