Hdu2053
Time limit1000 msMemory limit32768 kB
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 ).
InputEach test case contains only a number n ( 0< n<= 10^5) in a line.
OutputOutput the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).Sample Input
1 5
Sample Output
1 0
首先我们得知道,第n次开关改变n的倍数的灯,那么前n盏灯的状态不再改变。所以要知道无限次操作后第n盏灯的状态,只需计算n盏灯n次操作后的状态。
一开始想到的是模拟。时限是一秒,1秒大概能做10^8次操作,而题目的范围是10^10,不出意外TLE了。
1 #include <cstdio> 2 #include <iostream> 3 #include <queue> 4 #include <vector> 5 #include<string.h> 6 #include<map> 7 #include<bits/stdc++.h> 8 #define LL long long 9 #define maxn 100005 10 using namespace std; 11 int a[maxn]; 12 int n; 13 int main() 14 { 15 while(scanf("%d",&n)!=EOF) 16 { 17 memset(a,0,sizeof a); 18 for(int i=1;i<=n;i++) 19 { 20 for(int j=1;j<=n;j++) 21 { 22 if((j%i)==0) 23 a[j]=!a[j]; 24 } 25 } 26 printf("%d\n",a[n]); 27 } 28 return 0; 29 }
接着,想到第n盏的开关实际决定与它因数的个数,偶数个因数-0,奇数个因数-1.
1 #include <cstdio> 2 #include <iostream> 3 #include <queue> 4 #include <vector> 5 #include<string.h> 6 #include<map> 7 #include<bits/stdc++.h> 8 #define LL long long 9 #define maxn 100005 10 using namespace std; 11 int n,ans; 12 int main() 13 { 14 while(scanf("%d",&n)!=EOF) 15 { 16 ans=0;//记得初始化 17 for(int i=1;i<=n;i++) 18 if((n%i)==0) 19 ans++; 20 if(ans&1)printf("1\n"); 21 else printf("0\n"); 22 } 23 return 0; 24 }