TIOJ 1535 - 嘿!! Let's Go E-m-i-r-p!!

Link: http://tioj.ck.tp.edu.tw/problems/1535

質數建表然後判斷是不是Emirp,至於要建到多大的質數表就在本機跑跑看,我的寫法MAX必須是10的次方否則判斷反轉時會RE,於是我用個bitset壓記憶體就過了

AC code
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
#include<iostream>
#include<vector>
#include<bitset>
#define MAX 100000000
using namespace std;
typedef long long ll;
bitset<MAX+10> np;
vector<int> em;
int reverse(int i)
{
int t=0;
while(i!=0)
t=t*10+i%10, i/=10;
return t;
}
bool check(int n)
{
int t=reverse(n);
if(t==n)return false;
return !np[t];
}
int main()
{
for(int i=2;i<=MAX;i++)
if(!np[i])
for(ll j=(ll)i*i;j<=MAX;j+=i)
np[j]=1;
for(int i=0;i<=MAX;i++)
if(!np[i] && check(i))
em.push_back(i);

ios::sync_with_stdio(0);
cin.tie(0);
int T,n;
cin>>T;
while(T--)
{
cin>>n;
cout<<em[n-1]<<'\n';
}
}