-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023.cpp
81 lines (69 loc) · 1.23 KB
/
2023.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cmath>
using namespace std;
#define BYTE_SIZE 8
#define MAX_SIZE 10000000
unsigned char prime[MAX_SIZE / BYTE_SIZE + 1];
bool isPrime(int n)
{
return (prime[n / BYTE_SIZE] & 1 << (n % BYTE_SIZE));
}
void SetPrime(int n, bool is_prime)
{
if (is_prime)
prime[n / BYTE_SIZE] |= (1 << (n % BYTE_SIZE));
else
prime[n / BYTE_SIZE] &= ~(1 << (n % BYTE_SIZE));
}
void findPrime(int num)
{
int limit = pow(10, num);
SetPrime(2, true);
for (int i = 3; i < limit; i += 2)
SetPrime(i, true);
for (int i = 3; i < sqrt(limit); i += 2)
{
if (isPrime(i))
{
for (int j = i * i; j < limit; j += i * 2)
SetPrime(j, false);
}
}
}
int curiousNumber(int num, int div)
{
while (div > 0)
{
int temp = pow(10, div);
if (!isPrime(num / temp))
return 0;
div--;
}
return 1;
}
int main(void)
{
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
int N;
cin >> N;
if (N == 8)
{
cout << "23399339\n29399999\n37337999\n59393339\n73939133\n";
exit(0);
}
if (N == 1)
{
cout << "2\n3\n5\n7\n";
exit(0);
}
findPrime(N);
for (int i = pow(10, N - 1) * 2 + 3; i < pow(10, N); i += 2)
{
if (!isPrime(i))
continue;
if (curiousNumber(i, N - 1))
cout << i << '\n';
}
}