1015 Reversible Primes(20 分)

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
 
 #include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define wzf ((1 + sqrt(5.0)) / 2.0)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 1e4 + ; int n, r; bool is_prime(int n)
{
if (n == || n == ) return false;
for (int i = ; i * i <= n; ++ i)
if (n % i == ) return false;
return true;
} int main()
{
while (scanf("%d", &n), n >= )
{
scanf("%d", &r);
if (!is_prime(n))
{
printf("No\n");
continue;
}
int cnt = , ans = ;
stack <int> my_stack;
while (n)
{
my_stack.push(n % r);
n /= r;
}
while (my_stack.size())
{
ans += my_stack.top() * (pow(r, cnt));
++ cnt;
my_stack.pop();
}
if (!is_prime(ans))
printf("No\n");
else
printf("Yes\n");
}
return ;
}

最新文章

  1. iOS开发常用代码块
  2. js判断地址转向
  3. 微信公众号入门学习1_使用C#,ASP.NET APIController如何公众号接入服务器并启动开发者模式
  4. Atitit.木马病毒强制强行关闭360&#160;360tray.exe的方法
  5. 【128】Word中的VBA
  6. 【Access2007】将Excel表导入到Access2007在现有的表成
  7. C# 索引器简介
  8. oracle数据库知识点
  9. linux_samba服务安装
  10. Spring IOC知识点一网打尽!
  11. navicat for mysql 注册码,简简单单,一个搞定(蔡军帅亲测有效)
  12. Linux服务器---邮件服务器dovecot
  13. 【转】简明 Vim 练级攻略
  14. ios开发之--使用AFN上传3.1.0上传视频,不走成功回调原因及解决方法
  15. TI davinci DM6467通过串口0将UBL和u-boot写入NAND flash
  16. UOJ#172. 【WC2016】论战捆竹竿
  17. 【HDU 2586】LCA模板
  18. jquery源码解析:type,isPlainObject,parseHTML,parseXML,globalEval详解
  19. 求组合数的方法:转载自VincentCZW的博客
  20. Node——用http-proxy 做反向代理服务器

热门文章

  1. MYSQL中HEX、UNHEX函数
  2. PMBOK(第六版) PMP笔记——《十三》第十三章(项目干系人管理)
  3. [Luogu3797] 妖梦斩木棒
  4. 基于canvas实现钟表
  5. The All-in-One Note
  6. V2er - Best client for V2EX
  7. Springboot 系列(十五)如何编写自己的 Springboot starter
  8. 主流 CSS 布局(水平居中、垂直居中、居中 )
  9. Nginx 热部署和日志切割,你学会了吗?
  10. python语法入门之流程控制