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;
}
相关推荐
新手小新16 分钟前
C++游戏开发(2)
开发语言·前端·c++
杰克尼43 分钟前
11. 盛最多水的容器
算法·leetcode·职场和发展
程序员编程指南2 小时前
Qt 嵌入式界面优化技术
c语言·开发语言·c++·qt
程序员Xu3 小时前
【OD机试题解法笔记】查找接口成功率最优时间段
笔记·算法
云泽8083 小时前
数据结构前篇 - 深入解析数据结构之复杂度
c语言·开发语言·数据结构
逝雪Yuki3 小时前
数据结构与算法——字典(前缀)树的实现
数据结构·c++·字典树·前缀树·左程云
技术思考者3 小时前
Leetcode - 反转字符串
数据结构·算法·leetcode
卷卷的小趴菜学编程3 小时前
Qt-----初识
开发语言·c++·qt·sdk·qt介绍
程序员编程指南4 小时前
Qt 开发 IDE 插件开发指南
c语言·c++·ide·qt·elasticsearch
SKYDROID云卓小助手4 小时前
无人设备遥控器之多设备协同技术篇
网络·人工智能·嵌入式硬件·算法·信号处理