/**//* PROG: sprime LANG: C */ #include<stdio.h> #define max 100000 FILE * fin; FILE * fout; char s[max]; int p[max]; int k; void setPrime() { int i, j, c, t = max / 2; k = 0; s[1] = 1; for (i = 2; i < t; i++) { if (!s[i]) { p[k++] = i; for (j = 2; (c = i * j) < max; j++) { s[c] = 1; } } } } int IsPrime(int n) { int j, c; if (n < max) { if (s[n]) { return 0; } return 1; } else { for (j = 0; j < k; j++) { if (n % p[j] == 0) { return 0; } } return 1; } } void Dfs(int depth, int n, int m) { depth++; if (IsPrime(m)) { if (depth == n) { fprintf(fout, "%d\n", m); } else { m *= 10; Dfs(depth, n, m + 1); Dfs(depth, n, m + 3); Dfs(depth, n, m + 7); Dfs(depth, n, m + 9); } } }
int main() { fin = fopen("sprime.in", "r"); fout = fopen("sprime.out", "w"); int n, i; setPrime(); fscanf(fin, "%d", &n); Dfs(0, n, 2); Dfs(0, n, 3); Dfs(0, n, 5); Dfs(0, n, 7); return 0; }
|