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;
}
相关推荐
huang57914720 分钟前
哈希算法的抗碰撞机制与安全性增强策略4
算法
林浩杨_41 分钟前
SIGIR 2026|南京大学:Video-GAR:“通过生成 Query 来验证视频语义理解”的生成增强范式
论文阅读·人工智能·算法
Logic1011 小时前
C语言/数据结构位运算题解:异或XOR找出货船中的“独特载货量“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
Elsa️7461 小时前
算法一周刷题总结
c++·算法
风合星语1 小时前
2026 机器人工程实例(五):让 Allegro Hand 在 MuJoCo 中完成接触抓取——抓取状态机、稳定抬升与受控释放
c++·python·机器人·仿真
小羊没烦恼!1 小时前
Memory 记忆设计讨论:Agent Memory 的数据模型可以怎么设计
java·开发语言·前端·c++·c#
小小程序猴11 小时前
深圳乘路资讯AI培训怎么样?课程靠谱吗?——一套课程可信度评估模型与实证分析
人工智能·算法
云上先途2 小时前
标签化服务适合哪些人?常见适用场景一次讲清
大数据·人工智能·算法·音视频
welfare9622 小时前
新号别搞:内存管理+模板初阶+STL简介
开发语言·c++
彧azz2 小时前
DFS与BFS:图遍历的两大核心算法
数据结构·学习·算法·深度优先·广度优先