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;
}
相关推荐
少许极端4 分钟前
算法奇妙屋(十五)-BFS解决边权为1的最短路径问题
数据结构·算法·bfs·宽度优先·队列·图解算法·边权为1的最短路径问题
c骑着乌龟追兔子16 分钟前
Day 27 常见的降维算法
人工智能·算法·机器学习
hetao173383719 分钟前
2025-12-02~03 hetao1733837的刷题记录
c++·算法
田里的水稻20 分钟前
math_旋转变换
算法·几何学
“愿你如星辰如月”24 分钟前
C++11核心特性全解析
开发语言·c++
ada7_29 分钟前
LeetCode(python)——94.二叉
python·算法·leetcode·链表·职场和发展
广都--编程每日问31 分钟前
c++右键菜单统一转化文件为utf8编码
c++·windows·python
点云SLAM31 分钟前
C++包装器之类型擦除(Type Erasure)包装器之小对象优化(SBO, Small Buffer Optimization)示例(5)
c++·内存管理·c++高级应用·c++包装器·类型擦除包装器·内存小对象优化
AI视觉网奇31 分钟前
躯体驱动 算法学习笔记
人工智能·算法
不穿格子的程序员36 分钟前
从零开始写算法——普通数组类题:数组操作中的“翻转技巧”与“前后缀分解”
数据结构·算法