码迷,mamicode.com
首页 > 其他好文 > 详细

pat1015. Reversible Primes (20)

时间:2015-08-06 16:29:54      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

1015. Reversible Primes (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (< 105) and D (1 < D <= 10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line "Yes" if N is a reversible prime with radix D, or "No" if not.

Sample Input:
73 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No

提交代码

 

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <queue>
 5 #include <stack>
 6 #include <iostream>
 7 using namespace std;
 8 bool prime[100005];
 9 void getprime(int n){
10     memset(prime,false,sizeof(prime));
11     int i,j;
12     prime[2]=true;
13     for(i=3;i<=n;i+=2){
14         prime[i]=true;
15         for(j=3;j*j<=i;j++){
16             if(i%j==0){
17                 prime[i]=false;
18                 break;
19             }
20         }
21     }
22 }
23 int main(){
24     //freopen("D:\\INPUT.txt","r",stdin);
25     getprime(100005);
26     int n,d;
27     while(scanf("%d",&n)!=EOF){
28         if(n<0){
29             break;
30         }
31         scanf("%d",&d);
32         if(!prime[n]){
33             printf("No\n");
34             continue;
35         }
36         queue<int> q;
37         while(n){
38             q.push(n%d);
39             n/=d;
40         }
41         //cout<<n<<endl;
42         while(!q.empty()){
43             n*=d;
44             n+=q.front();
45             q.pop();
46         }
47         //cout<<n<<endl;
48         if(prime[n]){
49             printf("Yes\n");
50         }
51         else{
52             printf("No\n");
53         }
54     }
55     return 0;
56 }

 

pat1015. Reversible Primes (20)

标签:

原文地址:http://www.cnblogs.com/Deribs4/p/4708267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!