Emirp

来源:互联网 发布:excel数据列表创建列表 编辑:程序博客网 时间:2024/05/24 04:48
J.EmirpTime Limit: 5000 MSMemory Limit: 100000 KTotal Submit: 1195 (392 users)Total Accepted: 275 (264 users)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

枚举每个数,并把它翻过来判断是不是素数。最终的答案不会很大,第1000emirp也就70000左右。

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int a[100000];int b[1000];void we(){    a[0]=a[1]=1;    for(int i=2;i<=1000;i++)    {        if(!a[i])        {        for(int j=2*i;j<=100000;j+=i)        {            a[j]=1;        }        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {         we();        int n;        scanf("%d",&n);        int k=0;        for(int i=13;i<=100000;i++)        {int p=0;                int sum=0;            if(!a[i])            {                int y=1;                int ha;                ha=i;                memset(b,0,sizeof(b));                while(ha>0)                {                    b[p++]=(ha%10);                    ha=ha/10;                }                for(int j=p-1;j>=0;j--)                {                    sum+=b[j]*y;                    y*=10;                }                if(!a[sum])                {                    k++;                    if(k==n)                    {                        printf("%d\n",i);                        break;                    }                }            }        }    }}

0 0
原创粉丝点击