Emirp

来源:互联网 发布:哔哩哔哩动画mac下载 编辑:程序博客网 时间:2024/05/24 05:16
EmirpTime Limit: 5000 MSMemory Limit: 100000 KTotal Submit: 143(59 users)Total Accepted: 61(55 users)Rating: Special Judge: NoDescription

An emirp (prime spelled backwards) is a prime number that results in a different prime when its decimal digits are reversed.

The first five emirps are 13, 17, 31, 37, 71

Now Kim want to know the kth emirp. Help him.

Input

The first line is an integer T, describes the number of tests. Then T tests.

In each test, one line an integer k.

Output

For each test, output the kth emirp.

Sample Input
3123
Sample Output
131731
Hint

T<=10000

k<=1000

思路:就是找大于十的数使其本身和其反置都为素数,并且本身和其反置数不能相同。


#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;int a[1000000];int sushu(int n){    int i,j;    int k;    k=sqrt(n);    for(i=2;i<=k;i++)    {        if(n%i==0)            break;    }    if(i>k)        return 1;    else        return 0;}int main(){    int i,j,k,k1,b,c,d,t,sum;    for(i=13,j=1; i<=70529; i++)    {        k=0;        t=i;        while(t>0)        {            k=k*10+t%10;            t=t/10;        }        k1=i;        if(sushu(k)==1&&sushu(k1)==1&&k1!=k)        {            a[j]=k1;            j++;        }    }    int n,m;    scanf("%d",&n);    while(n--)    {        scanf("%d",&m);        printf("%d\n",a[m]);    }}