1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 using namespace std; 6 bool visit[10100000]; 7 int prime[10000000]; 8 9 10 void init_prim() 11 { 12 memset(visit, true, sizeof(visit)); 13 int num = 0; 14 for (int i = 2; i <= 10000000; ++i) 15 { 16 if (visit[i] == true) 17 { 18 num++; 19 prime[num] = i; 20 } 21 for (int j = 1; ((j <= num) && (i * prime[j] <= 10000000)); ++j) 22 { 23 visit[i * prime[j]] = false; 24 if (i % prime[j] == 0) break; 25 } 26 } 27 } 28 int main() { 29 memset(prime, 0, sizeof(prime)); 30 int T, n; 31 init_prim(); 32 scanf("%d", &T); 33 while(T--) { 34 scanf("%d", &n); 35 bool flag = true; 36 if(n < 1e6) { 37 if(n == 4) { 38 printf("%d\n", 2); 39 } else if(visit[n] == true){ 40 printf("%d\n", n-1); 41 } else { 42 printf("%d\n", 0); 43 } 44 } else { 45 for(int i = 1; prime[i] <= sqrt(n); ++i) { 46 if(n % prime[i] == 0) { 47 flag = false; 48 printf("%d\n", 0); 49 break; 50 } 51 } 52 if(flag) { 53 printf("%d\n", n-1); 54 } 55 } 56 } 57 return 0; 58 }
第一题
时间: 2024-11-04 15:14:31