USACO 1.5 Prime Palindromes

1 /*
2 ID: jiafeim1
3 PROG: pprime
4 LANG: C++
5 */
6 #include <iostream>
7 #include <fstream>
8 #include <algorithm>
9
10 using namespace std;
11
12 #include <cmath>
13 long pp[200000];
14 long top = 0;
15
16 bool check(long num)
17 {
18 if(num%2 == 0) return false;
19 long end = std::sqrt((float)num)+1;
20 for(int i=3;i<=end;i+=2)
21 {
22 if(num%i==0) return false;
23 }
24 return true;
25 }
26
27 int main()
28 {
29 ofstream fout ("pprime.out");
30 ifstream fin ("pprime.in");
31 pp[0] = 5;
32 pp[1] = 7;
33 pp[2] = 11;
34 top = 3;
35 for (int a1 = 1; a1 <= 9; a1 += 2)
36 {
37 for (int a2 = 0 ; a2 <= 9; ++a2)
38 {
39 pp[top++] = a1 + a2 * 10 + a1 * 100;
40 }
41 }
42 for (int a1 = 1; a1 <= 9; a1 += 2)
43 {
44 for (int a2 = 0 ; a2 <= 9; ++a2)
45 {
46 for (int a3 = 0; a3 <= 9; ++a3)
47 {
48 pp[top++] = a1 + a2 * 10 + a3 * 100 + a2 * 1000 + a1 * 10000;
49 }
50 }
51 }
52 for (int a1 = 1; a1 <= 9; a1 += 2)
53 {
54 for (int a2 = 0 ; a2 <= 9; ++a2)
55 {
56 for (int a3 = 0; a3 <= 9; ++a3)
57 {
58 for (int a4 = 0 ; a4 <= 9 ; ++a4)
59 {
60 pp[top++] = a1 + a2 * 10 + a3 * 100 + a4 * 1000 + a3 * 10000 + a2 * 100000 + a1 * 1000000;
61
62 }
63 }
64 }
65 }
66
67 long min,max;
68 fin>>min>>max;
69 int start;
70 for(start = 0;start!=top;++start)
71 {
72 if(pp[start]>=min) break;
73 }
74
75 for(;start!=top;++start)
76 {
77 if(pp[start]>max) break;
78 if(check(pp[start]))
79 fout<<pp[start]<<endl;
80 }
81
82 fin.close();
83 fout.close();
84 return 0;
85 }
posted @ 2011-05-02 14:13  幻魇  阅读(191)  评论(0编辑  收藏  举报