1015 Reversible Primes

1015 Reversible Primes

分数 20

全屏浏览

切换布局

作者 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 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (<10

5

) 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

1.分析

1.要判断原数是否是素数,还要判断反转后的数是否为素数

2.代码

cpp 复制代码
#include<iostream>
#include<cmath>
using namespace std;
const int MAX=1e6+10;
int N,D,re[MAX];
bool check(int x){           //判断是否为素数
    for(int i=2;i<=x/i;i++){
        if(x%i==0) return false;
    }
    return true;
}
int main(){
    while(cin>>N){
        if(N<0) break;        //判断输入结束
        else cin>>D;
        if(N<2||!check(N)) {       //判断原数
            cout<<"No"<<endl;
            continue;
        }
        int num=0;
        while(N){                   //反转
            re[num++]=N%D;
            N/=D;
        }
        for(int i=num-1;i>=0;i--){
            N+=pow(D,num-i-1)*re[i];
        }
        if(N>=2&&check(N)) cout<<"Yes"<<endl;      //判断
        else cout<<"No"<<endl;
    }
    return 0;
}
相关推荐
karmueo462 小时前
视频序列和射频信号多模态融合算法Fusion-Vital解读
算法·音视频·多模态
小汉堡编程3 小时前
数据结构——vector数组c++(超详细)
数据结构·c++
写代码的小球5 小时前
求模运算符c
算法
雾里看山6 小时前
顺序表VS单链表VS带头双向循环链表
数据结构·链表
tan180°8 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
大千AI助手8 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
好好研究9 小时前
学习栈和队列的插入和删除操作
数据结构·学习
彭祥.9 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk10 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节