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;
}
相关推荐
银河小铁骑plus3 分钟前
力扣hot100_贪心算法
算法·leetcode·贪心算法
小郝 小郝6 分钟前
(C语言)指针运算 习题练习1.2(压轴难题)
java·开发语言·算法
welkin19 分钟前
算法区间合并问题
前端·算法
星途码客40 分钟前
C++位运算精要:高效解题的利器
java·c++·算法
东雁西飞1 小时前
MATLAB 控制系统设计与仿真 - 33
开发语言·算法·matlab·机器人·自动控制
Joyee6912 小时前
文本领域的在线协作引擎——OT 算法的原理与应用
算法
周Echo周2 小时前
5、vim编辑和shell编程【超详细】
java·linux·c++·后端·编辑器·vim
lisw052 小时前
排序算法可视化工具——基于React的交互式应用
算法·react.js·排序算法
榆榆欸2 小时前
6.实现 Reactor 模式的 EventLoop 和 Server 类
linux·服务器·网络·c++·tcp/ip
奋进的小暄2 小时前
贪心算法(13)(java)合并区间
算法