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] = false;
    if (len < 3) return;
    primes[2] = true;

    for (int i=2; i<len; i++) {
        if (!primes[i]) continue;
        int v = i * 2;
        while (v < len) {
            primes[v] = false;
            v += i;
        }
    }

}

int inv_value(int n, int radix) {
    int base = 1;
    int v = 0;
    int r = 0;
    vector<int> rs;
    while (n) {
        r = n % radix;
        n = n / radix;
        rs.push_back(r);
    }
    for (int i=rs.size() - 1; i>=0; i--) {
        v += rs[i] * base;
        base *= radix;
    }
    return v;
}

int main() {
    const int MAX_N = 100000;
    vector<char> primes(MAX_N + 1, true);
    init_primes(primes);

    int num, radix;

    for (;;) {
        scanf("%d", &num);
        if (num < 0) break;
        scanf("%d", &radix);

        if (!primes[num]) {
            printf("No\n");
            continue;
        }
        int iv = inv_value(num, radix);
        if (!primes[iv]) {
            printf("No\n");
        } else {
            printf("Yes\n");
        }
    }

    return 0;
}

每日一练

时间: 2024-10-06 01:12:15

PAT 1015. Reversible Primes的相关文章

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 (判断素数)

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) ——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 (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

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 be

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

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