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

1015. Reversible Primes (20)(数论:素数、进制转换)

时间:2017-05-14 00:49:11      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:finish   radix   pre   pac   sam   its   not   inpu   focus   

A 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 <bits/stdc++.h>
 2 using namespace std;
 3 int n , d;
 4 static const int MAXN = 1e5 + 10;
 5 bool is_prime[MAXN];
 6 int prime[MAXN];
 7 int pos = 0;
 8 void Prime()
 9 {
10     for(int i = 2 ; i < MAXN ; ++i)
11         is_prime[i] = 1;
12     for(int i = 2 ; i < MAXN ; ++i)
13     {
14         if(is_prime[i])
15             prime[++pos] = i;
16         for(int j = 1 ; j <= pos ; ++j)
17         {
18             if(i * prime[j] >= MAXN)
19                 break;
20             is_prime[i * prime[j]] = 0;
21             if(i % prime[j] == 0)
22                 break;
23         }
24     }
25 }
26 int Reverse()
27 {
28     int sum = 0;
29     do
30     {
31         sum = sum * d + n % d;
32         n /= d;
33     }while(n != 0);
34     return sum;
35 }
36 int main()
37 {
38     Prime();
39     while(~scanf("%d" , &n))
40     {
41         if(n < 0)
42             return 0;
43         scanf("%d" , &d);
44         if(is_prime[n] && is_prime[Reverse()])
45             puts("Yes");
46         else
47             puts("No");
48     }
49 }
View Code

 

1015. Reversible Primes (20)(数论:素数、进制转换)

标签:finish   radix   pre   pac   sam   its   not   inpu   focus   

原文地址:http://www.cnblogs.com/jianglingxin/p/6850707.html

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