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

采用素数筛选法,若$i$为素数,则$i*j$不是素数,其中$j=2,3,....$。这样可以不用判断哪个数为素数,因为非素数的都必定会被筛选出来。

代码

#include <stdio.h>
#include <math.h> char primerTable[500000]; void calculatePrimerTable();
int num2array(int,int,int*);
int array2num(int*,int,int);
int main()
{
    calculatePrimerTable();
    int N,D,len;
    int data[32];
    while(scanf("%d",&N)){
        if(N < 0)
            break;
        scanf("%d",&D);
        if(primerTable[N] == 'N'){
            printf("No\n");
            continue;
        }
        len = num2array(N,D,data);
        if(primerTable[array2num(data,len,D)] == 'Y')
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
} void calculatePrimerTable()
{
    int i;
    for(i=2;i<500000;i++){
        if(primerTable[i] != 'N'){
            primerTable[i] = 'Y';
            int j,n;
            for(j=2,n=2*i;n<500000;++j,n=j*i){
                primerTable[n] = 'N';
            }          
        }
    }
} int num2array(int n,int base,int *s)
{
    if(n < 0)
        return 0;
    else if(n == 0){
        s[0] = 0;
        return 1;
    }
    int len = 0;
    while(n){
        s[len++] = n % base;
        n = n / base;
    }
    return len;
} int array2num(int *s,int len,int base)
{
    int i,n = 0;
    for(i=0;i<len;++i){
        n = n * base + s[i];
    }
    return n;
}

PAT 1015的更多相关文章

  1. PAT 1015 Reversible Primes

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  2. PAT 1015 Reversible Primes[求d进制下的逆][简单]

    1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...

  3. PAT 1015 德才论 (25)(代码+思路)

    1015 德才论 (25)(25 分)提问 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子, ...

  4. PAT——1015. 德才论

    宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人 ...

  5. pat 1015 Reversible Primes(20 分)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  6. PAT 1015. 德才论 (25) JAVA

    宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子 ...

  7. PAT 1015. 德才论 (25)

    宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子 ...

  8. PAT 1015 德才论

    https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312 宋代史学家司马光在<资治通鉴>中 ...

  9. PAT 1015 Reversible Primes (判断素数)

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

随机推荐

  1. apache开源项目--TIKA

    Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提供了便利的扩展 ...

  2. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  3. C++ 学习资料搜寻与学习(第一期)(未完待续)

    一.图形图像类 [Visual C++]vs2008/2005正确打开vs2010所创建项目的几种方法 jlins 2012-04-12 14:38 [Visual C++]关于无法打开包括文件:“S ...

  4. xmbc 资源

    http://raspberrypi.diandian.com/post/2013-02-25/40048470423 http://blog.csdn.net/lincyang/article/de ...

  5. Stm32外围模块编程初始化步骤

    Stm32外围模块编程初始化步骤: 一.外部中断 1)初始化 IO 口为输入. 这一步设置你要作为外部中断输入的 IO 口的状态,可以设置为上拉/下拉输入,也可以设置为浮空输入,但浮空的时候外部一定要 ...

  6. 【暑假】[深入动态规划]UVAlive 3983 Robotruck

     UVAlive 3983 Robotruck 题目: Robotruck   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format ...

  7. LightOJ 1422 Halloween Costumes

    dp[i]][j]=min(dp[i+1][j]+1,dp[i+1][k-1]+dp[k][j]) 表示第i天到j的最小数量.如果第i天的衣服只自己穿的话,不考虑后面的就是dp[i][j]=dp[i+ ...

  8. inter

    网卡不稳定的罪魁祸首   近期新上的DB SERVER服务器,在压测中发现网卡很不稳定,压力测试刚刚进行十几分钟后,服务器反应就变得非常慢,PING的时候经常丢包而且SSH连接也时断时 续.刚开始以为 ...

  9. 【C语言】-循环结构-for语句

    for语句: for (表达式1;表达式2;表达式3) { 循环语句 } 表达式1 给循环变量赋初值 表达式2 为循环条件 表达式3 用来修改循环变量的值,称为循环步长. for语句的执行流程: 例: ...

  10. url 中文编解码

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...