素数系列——反素数

来源:互联网 发布:网络翻唱女歌手曼里 编辑:程序博客网 时间:2024/06/05 01:07
Emirp

Time Limit: 5000 MSMemory Limit: 100000 KTotal Submit: 147(60 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

Source

"科林明伦杯"哈尔滨理工大学第六届程序设计团队赛



思路:

题意很简单,找出所有素数中,翻转后也是素数(反素数),并且和原数不相等的,存入数组中

每次先输入总情况数,每个情况输入一个具体位置,输出该位置的对应答案素数

找出每个素数存入数组,再依次判断是否符合题意。但容易超时。

找素数时做好标记,然后进行记忆化遍历,符合题意存入数组,最后直接输出对应答案


代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[1000010],b[2010],c[2010];int ans=0;void get(){int i,j,s,m,ans;memset(a,0,sizeof(a));a[0]=a[1]=1;for(i=2;i*i<1000005;i++){if(!a[i])for(j=i*2;j<1000005;j+=i)a[j]=1;}ans=1;for(i=13;i<1000010;i++){if(ans>1000)break;if(!a[i]){  m=i;  s=0;    while(m)    {        int flag=m%10;        m/=10;        s=s*10+flag;    }  if(!a[s]&&s!=i)  b[ans++]=i;}}}int main(){int t,n;get();scanf("%d",&t);while(t--){scanf("%d",&n);printf("%d\n",b[n]);}return 0;}


也有写出来超时的情况,可以把结果都打出来直接存到数组中,标程就是这样。。。

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int main(){    int a[1009]={0,13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389,701,709,733,739,743,751,761,769,907,937,941,953,967,971,983,991,1009,1021,1031,1033,1061,1069,1091,1097,1103,1109,1151,1153,1181,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1279,1283,1301,1321,1381,1399,1409,1429,1439,1453,1471,1487,1499,1511,1523,1559,1583,1597,1601,1619,1657,1669,1723,1733,1741,1753,1789,1811,1831,1847,1867,1879,1901,1913,1933,1949,1979,3011,3019,3023,3049,3067,3083,3089,3109,3121,3163,3169,3191,3203,3221,3251,3257,3271,3299,3301,3319,3343,3347,3359,3371,3373,3389,3391,3407,3433,3463,3467,3469,3511,3527,3541,3571,3583,3613,3643,3697,3719,3733,3767,3803,3821,3851,3853,3889,3911,3917,3929,7027,7043,7057,7121,7177,7187,7193,7207,7219,7229,7253,7297,7321,7349,7433,7457,7459,7481,7507,7523,7529,7547,7561,7577,7589,7603,7643,7649,7673,7681,7687,7699,7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963,9001,9011,9013,9029,9041,9103,9127,9133,9161,9173,9209,9221,9227,9241,9257,9293,9341,9349,9403,9421,9437,9439,9467,9479,9491,9497,9521,9533,9547,9551,9601,9613,9643,9661,9679,9721,9749,9769,9781,9787,9791,9803,9833,9857,9871,9883,9923,9931,9941,9967,10007,10009,10039,10061,10067,10069,10079,10091,10151,10159,10177,10247,10253,10273,10321,10333,10343,10391,10429,10453,10457,10459,10487,10499,10613,10639,10651,10711,10739,10781,10853,10859,10867,10889,10891,10909,10939,10987,10993,11003,11057,11071,11083,11149,11159,11161,11197,11243,11257,11329,11353,11423,11447,11489,11497,11551,11579,11587,11593,11621,11657,11677,11699,11701,11717,11719,11731,11777,11779,11783,11789,11833,11839,11897,11903,11909,11923,11927,11933,11939,11953,11959,11969,11971,11981,12071,12073,12107,12109,12113,12119,12149,12227,12241,12253,12269,12289,12301,12323,12373,12437,12491,12547,12553,12577,12611,12619,12641,12659,12689,12697,12713,12743,12757,12763,12799,12809,12829,12841,12893,12907,12919,12983,13009,13043,13147,13151,13159,13163,13259,13267,13291,13297,13337,13441,13457,13469,13477,13499,13513,13523,13553,13591,13597,13619,13693,13697,13709,13711,13751,13757,13759,13781,13789,13829,13841,13873,13903,13933,13963,14029,14057,14071,14081,14087,14107,14143,14153,14177,14207,14221,14251,14293,14303,14323,14327,14387,14423,14431,14447,14449,14479,14519,14549,14551,14557,14563,14591,14593,14621,14629,14633,14657,14713,14717,14821,14831,14843,14879,14891,14897,14923,14929,14939,14947,14957,15013,15053,15091,15101,15131,15139,15149,15227,15241,15263,15289,15299,15307,15349,15377,15383,15461,15493,15497,15511,15527,15541,15601,15643,15649,15661,15667,15679,15683,15731,15733,15737,15791,15803,15907,15919,15937,15973,16001,16007,16063,16073,16103,16111,16127,16193,16217,16223,16249,16267,16427,16433,16451,16453,16481,16493,16547,16567,16573,16603,16651,16691,16699,16729,16747,16763,16829,16879,16883,16937,16943,16979,17011,17021,17033,17041,17047,17117,17203,17207,17209,17383,17393,17417,17443,17467,17477,17491,17519,17573,17579,17599,17627,17669,17681,17683,17713,17737,17747,17749,17827,17839,17863,17903,17909,17911,17923,17939,17959,18013,18041,18077,18089,18133,18169,18191,18199,18253,18269,18307,18329,18353,18379,18413,18427,18439,18461,18539,18593,18637,18671,18691,18701,18719,18731,18743,18749,18757,18773,18787,18803,18859,18899,18911,18913,19001,19013,19037,19051,19163,19181,19219,19231,19237,19249,19301,19333,19403,19421,19423,19471,19477,19489,19531,19541,19543,19553,19577,19661,19681,19687,19697,19751,19759,19763,19793,19801,19813,19841,19913,19973,30011,30029,30059,30139,30161,30197,30223,30259,30271,30319,30323,30341,30367,30467,30491,30517,30529,30539,30557,30593,30643,30649,30661,30757,30809,30851,30853,30859,30881,30911,30931,30949,30971,30983,31033,31051,31063,31069,31081,31091,31121,31139,31183,31193,31223,31259,31267,31277,31307,31327,31393,31481,31531,31543,31601,31627,31643,31649,31721,31723,31741,31771,31799,31859,31873,31891,31907,31957,31963,31981,31991,32009,32077,32099,32143,32173,32189,32203,32213,32233,32257,32261,32299,32303,32321,32341,32353,32369,32377,32411,32441,32467,32479,32491,32497,32531,32537,32563,32579,32633,32647,32687,32693,32713,32749,32783,32869,32887,32911,32933,32939,32941,32971,32983,32999,33013,33029,33049,33071,33181,33199,33223,33287,33301,33317,33329,33391,33461,33589,33617,33623,33641,33751,33767,33809,33811,33857,33863,33911,33923,33931,34031,34123,34129,34141,34147,34159,34211,34267,34273,34301,34367,34469,34471,34513,34549,34583,34589,34591,34603,34613,34651,34673,34687,34721,34757,34781,34807,34841,34847,34897,34919,34961,34963,35027,35051,35069,35083,35099,35117,35129,35141,35149,35159,35201,35221,35227,35257,35267,35281,35311,35317,35323,35327,35363,35381,35401,35419,35437,35447,35461,35521,35531,35537,35569,35591,35729,35801,35803,35911,35969,35983,35993,36013,36037,36061,36097,36107,36109,36131,36187,36191,36209,36217,36251,36269,36277,36353,36373,36467,36473,36479,36523,36541,36599,36607,36721,36739,36761,36791,36809,36833,36871,36877,36913,36931,36943,36973,37021,37061,37123,37199,37201,37243,37307,37309,37321,37363,37379,37409,37463,37489,37507,37547,37549,37561,37571,37589,37619,37643,37781,37813,37831,37847,37889,37897,37951,37963,37991,37997,38011,38039,38053,38113,38119,38219,38239,38287,38327,38329,38351,38371,38377,38393,38449,38459,38543,38557,38629,38639,38651,38671,38707,38711,38723,38737,38861,38867,38903,38917,38921,38923,38953,38977,38993,39047,39113,39119,39157,39161,39217,39241,39313,39359,39371,39383,39397,39419,39439,39451,39461,39503,39511,39541,39581,39623,39631,39709,39749,39791,39799,39821,39827,39829,39839,39869,39877,39887,39901,39929,39953,39983,39989,70001,70009,70061,70079,70121,70141,70163,70241,70249,70271,70289,70313,70327,70351,70373,70381,70439,70457,70489,70529};    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        printf("%d\n",a[n]);    }}




革命尚未成功!

0 0
原创粉丝点击