[BOJ] 1747 소수&팰린드롬

Time Lapse :13min 2sec

1747.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
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
#define MAXNUM 10000000
bool is_Prime[5000001];
bool isPall(int n) {
string num = to_string(n);
int maxn = num.length()>>1;
for(int i = 0; i <= maxn; i++) {
if(num[i] != num[num.length()-1-i])
return false;
}
return true;
}
int main(int argc, char** argv){
int n;
memset(is_Prime,true,sizeof(is_Prime));
is_Prime[0] = false;
scanf("%d",&n);
if(n<=2) {
printf("2");
exit(0);
}
for(int i=3; i*i<=MAXNUM;i+=2){
if(is_Prime[i>>1]){
for(int j = i*i; j<=MAXNUM; j += (i<<1))
is_Prime[j>>1] = false;
}
}
for(int i=3;i<=MAXNUM; i+=2){
if(i >= n && is_Prime[i>>1] && isPall(i)) {
printf("%d",i);
exit(0);
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/01/PS/BOJ/1747/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.