题目大意,令4n+1的数叫H数,H数素数x的定义是只能被x=1*h(h是H数),其他都叫合数,特别的,当一个数只能被两个H素数乘积得到时,叫H-semi数
做法,筛法暴力打表,记得要打表不然会TLE
1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 #define MAX_N 1000100 5 6 using namespace std; 7 8 static int H_Semi[MAX_N], H_Semi_Flag[MAX_N], H_Semi_Sum[MAX_N]; 9 10 void Inivilize(void); 11 12 int main(void) 13 { 14 int range; 15 Inivilize(); 16 while (~scanf("%d", &range)) 17 { 18 if (range == 0) break; 19 printf("%d %d ", range, H_Semi_Sum[range]); 20 } 21 return 0; 22 } 23 24 void Inivilize(void) 25 { 26 //-1不是H数 ,0表示是H_P,1表示H_C,2表示H_s 27 int j, i, ans = 0; 28 memset(H_Semi_Flag, -1, sizeof(H_Semi_Flag)); 29 for (i = 1; 4 * i + 1 <= 1000001; i++) 30 H_Semi_Flag[4 * i + 1] = 0; 31 for (i = 1; 4 * i + 1 <= 1000001; i++) 32 { 33 for (j = 1; (4 * i + 1) * (4 * j + 1) <= 1000001 && j <= i; j++) 34 { 35 if (H_Semi_Flag[(4 * j + 1)] == 0) 36 { 37 if (H_Semi_Flag[(4 * i + 1)] == 0) 38 { 39 if (H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] == 0) 40 H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] = 2; 41 else if (H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] == 2) 42 continue; 43 } 44 else if (H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] == 2) 45 H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] = 1; 46 } 47 else if (H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] == 0 || H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] == 2) 48 H_Semi_Flag[(4 * i + 1) * (4 * j + 1)] = 1; 49 } 50 } 51 for (int i = 1; i <= 1000001; i++) 52 { 53 if (H_Semi_Flag[i] == 2) 54 ans++; 55 H_Semi_Sum[i] = ans; 56 } 57 }