前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 1015 Reversible Primes

PAT 1015 Reversible Primes

作者头像
ShenduCC
发布2018-04-27 10:20:57
5000
发布2018-04-27 10:20:57
举报
文章被收录于专栏:算法修养算法修养

1015. Reversible Primes (20)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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:

代码语言:javascript
复制
73 10
23 2
23 10
-2

Sample Output:

代码语言:javascript
复制
Yes
Yes
No#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue>

using namespace std;
int n,d;
int bin[105];
bool judge(int x)
{
  if(x==1||x==0) return false;
  if(x==2) return true;
  for(int i=2;i*i<=x;i++)
  {
    if(x%i==0)
      return false;
  }
  return true;
}
void fun(int x,int d)
{
  int cnt=0;
  while(x>=d)
  {
    bin[cnt++]=x%d;
    x/=d;
  }
  if(x>0)
    bin[cnt++]=x;
  int num=0;
  for(int i=0;i<cnt;i++)
  {
    num*=d;
    num+=bin[i];
  }
  if(!judge(num))
    printf("No\n");
  else
    printf("Yes\n");
}
int main()
{
  while(scanf("%d",&n)!=EOF)
  {
    if(n<0)
      break;
    scanf("%d",&d);
    if(!judge(n))
    {
      printf("No\n");
      continue;
      
    }
    fun(n,d);
  }
  return 0;

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1015. Reversible Primes (20)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档