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 (<) and D (1), 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
思路:先判断自身是否是素数,再转换为对应进制的数,再判断是否是素数。
 #include <iostream>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;
int n,d;
int _prime(int x)
{
if(x==) return ;
int flag=;
for(int i=;i*i<=x;i++){
if(x%i==){
flag=;
break;
}
}
return flag;
}
int _deal(int n,int d)
{
stack<int> s;
while(n){
s.push(n%d);
n/=d;
}
int sum=,t=;
while(!s.empty()){
sum+=s.top()*t;
t*=d;
s.pop();
}
return sum;
}
int main()
{
while(cin>>n&&n>){
cin>>d;
int flag;
if(!_prime(n)) flag=;
else{
if(_prime(_deal(n,d))) flag=;
else flag=;
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return ;
}

最新文章

  1. HTML编辑器
  2. 查看域名对应的ip地址
  3. 构建 XCache 的基本步骤
  4. tomcat源码导入eclipse
  5. jfinal拦截器301跳转
  6. xapian安装
  7. 【python自动化第二篇:python入门】
  8. 客户端持久化解决方案:indexedDB
  9. PhoneGap笔记-01 基本使用
  10. cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(五)-地图卷动
  11. 何为PostgreSQL?
  12. iOS----KVC和KVO 详解
  13. C#窗体加载和控件加载不同步导致控件闪烁
  14. 浏览器本地数据存储解决方案以及cookie的坑
  15. Python全栈开发之路 【第五篇】:Python基础之函数进阶(装饰器、生成器&amp;迭代器)
  16. key-event
  17. socketserver实现FTP
  18. lsof and dynamic array in bash/shell
  19. MySQL锁分类
  20. Hadoop主要架构

热门文章

  1. Python - os.walk()详细使用
  2. java 子线程定时去更改主线程的变量
  3. [Python-memcached]Python操作memcached
  4. MySQL 数据库的提速器-写缓存(Change Buffer)
  5. pikachu-XXE(xml external entity-injection)
  6. Java基础之六、Java编程思想(8-10)
  7. JS中new的实现原理及重写
  8. Mac搭建本地服务器并映射到外网
  9. 【python基础语法】第5天作业练习题
  10. 声明式服务调用:Spring Cloud Feign