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;
}
相关推荐
MC皮蛋侠客11 小时前
C++编译死机排查工具与实战指南
c++
tang&11 小时前
双指针算法:化繁为简的优雅解法
数据结构·c++·算法
爱装代码的小瓶子11 小时前
【c++知识铺子】封装map和set(详细版)
android·java·c++
Aaron158811 小时前
RFSOC+VU13P在无线信道模拟中的技术应用分析
数据结构·人工智能·算法·fpga开发·硬件架构·硬件工程·射频工程
明洞日记11 小时前
【VTK手册026】高性能网格简化——vtkQuadricClustering 深度解析
c++·图像处理·vtk·图形渲染
咸鱼加辣11 小时前
“刻意强调” O(1)
数据结构·算法
南烟斋..12 小时前
Linux进程管理完全指南:创建、终止、回收与替换
linux·算法
xiaoye-duck12 小时前
C++入门基础指南:引用全解析(从入门到精通)
c++
点我头像干啥12 小时前
机器学习算法之动量法:优化梯度下降的“惯性”策略
人工智能·神经网络·算法·机器学习
XFF不秃头12 小时前
力扣刷题笔记-下一个排列
c++·笔记·算法·leetcode