Mathematics:Semi-prime H-numbers(POJ 3292)


              

              Semi-prime H-numbers

  題目大意,令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\n", 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 }


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com