• 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
    ");
                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
    ");
                else
                    printf("No
    ");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    在不给spring管理的类中获取类
    poi操作excel
    闭包
    输入url的过程发生了什么?
    跨域
    函数节流-防抖函数
    预解析-案例
    移动端适配方案
    实现元素水平居中和垂直居中的几种方法
    css小知识点
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9670932.html
Copyright © 2020-2023  润新知