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