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:
1 2 3 4 |
73 10 23 2 23 10 -2 |
Sample Output:
1 2 3 |
Yes Yes No |
这道题忘了是什么时候做的了(反正也很简单……细节已经忘记了),看到自己的repo里有,顺手更新了上来。
不过感觉自己写的那句 while(scanf("%d",&n),n>=0) 有种好鬼畜的感觉……
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<stdio.h> #include<string.h> #include<math.h> int is_prime(int n) { int i; if(n==1||n==0) return 0; for(i=2;i<=sqrt(n);i++) { if(n%i==0) return 0; } return 1; } int main() { int n,d,temp,i; char reversed[100],*p_reversed; while(scanf("%d",&n),n>=0) { p_reversed=reversed; scanf("%d",&d); if(!is_prime(n)) { printf("No\n"); } else { temp=n; do { *(p_reversed++)=temp%d+'0'; temp/=d; } while(temp>0); temp=0; *p_reversed='\0'; for(i=0;i<strlen(reversed);i++) { temp+=(reversed[i]-'0')*pow(d,strlen(reversed)-i-1); } if(is_prime(temp)) { printf("Yes\n"); } else { printf("No\n"); } } } } |