PAT 甲级 1015 Reversible Primes

https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000

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 (<) and D (1), 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

时间复杂度:$ O(\sqrt{N}) $

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;

int prime(int x) {
    if(x == 1) return 0;
    if(x == 2) return true;
    for(int i = 2; i * i <= x; i ++)
        if(x % i == 0)
        return 0;
    return 1;
}

int main() {
    int N, D;
    int num[maxn];
    while(~scanf("%d", &N)) {
        if(!N) break;
        scanf("%d", &D);
        if(prime(N) == 0) {
            printf("No\n");
            continue;
        }
        else {
            int cnt = 0;
            do{
                num[cnt ++] = N % D;
                N /= D;
            }while(N);

            int sum = 0, k = 0;
            for(int i = cnt - 1; i >= 0; i --) {
                sum += num[i] * pow(D, k);
                k ++;
            }

            if(prime(sum))
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9670932.html

时间: 2024-10-14 01:08:53

PAT 甲级 1015 Reversible Primes的相关文章

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) 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

PAT:1015. Reversible Primes (20) AC

#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; int D[111]; //存放拆解的数字 int DI=0; //D的数组下标 bool isPrime(int n) { if(n<=1) return 0; int sqr=(int)sqrt(1.0*n); for(int i=2 ; i<sqr+1 ; ++i) if(0==n%i) return

1015. Reversible Primes (20) ——PAT (Advanced Level) Practise

题目信息: 1015. Reversible Primes (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT 1015 Reversible Primes[求d进制下的逆][简单]

1015 Reversible Primes (20)(20 分)提问 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

pat 1015 Reversible Primes(20 分)

1015 Reversible Primes(20 分) 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 a

PAT 1015. Reversible Primes

#include <cstdio> #include <cstdlib> #include <cstring> #include <vector> using namespace std; void init_primes(vector<char>& primes) { int len = primes.size(); for (int i=0; i<len && i<2; i++) primes[i] = f

PAT 1015 Reversible Primes (判断素数)

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 (&

1015. Reversible Primes

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 (&

1015. Reversible Primes (20)

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 (&