PAT_A1015 Reversible Primes

发布时间 2023-11-01 09:48:16作者: 永无荒城

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
#include<bits/stdc++.h>
using namespace std;
const int N = 100000+5;
int p[N], prim[N], c, d[111], dl;
void findPrim(int n){
	for(int i = 2; i <= n; i++)
		if(p[i]==0){
			prim[c++]=i;
			for(int j = i+i; j <= n; j+=i)p[j]=1;
		}
}

int reverRadix(int n, int r){
	dl=0;
	while(n){
		d[dl++] = n%r;
		n /= r;
	}
	for(int i = 0; i < dl; i++)
		n = n*r + d[i];
	return n;
}

int main(){
	findPrim(N);
	p[1] = 1; //测试点1报错
	while(1){
		int n, d;
		scanf("%d", &n);
		if(n < 0) return 0;
		scanf("%d", &d);
		if(p[n]) printf("No\n");
		else if(p[reverRadix(n, d)]) printf("No\n");
		else printf("Yes\n");
	}
	return 0;
}

总结
1、 #数学问题
测试点1报错,原因是1不是素数也不是合数,需要添加特判p[1] = 1;