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. Windows安装weblogic

    WebLogic安装结束 以下是进入MyEclipse启动配置WebLogic

  2. 用Apache Kafka构建流数据平台

    近来,有许多关于“流处理”和“事件数据”的讨论,它们往往都与像Kafka.Storm或Samza这样的技术相关.但并不是每个人都知道如何将这种技术引入他们自己的技术栈.于是,Confluent联合创始 ...

  3. HDU 4003-Find Metal Mineral(树状背包)

    题意: n个节点的树给出每个边的权值,有k个机器人,求由机器人走完所有节点的最小花费(所有机器人开始在根节点) 分析: 仔细看了几遍例题后,发现这个题的状态很巧妙,先从整体考虑,一个机器人走完所有边回 ...

  4. 软件测试模型汇总-V模型,W模型,X模型,H模型

    V模型 在软件测试方面,V模型是最广为人知的模型,尽管很多富有实际经验的测试人员还是不太熟悉V模型,或者其它的模型.V模型已存在了很长时间,和瀑布开发模型有着一些共同的特性,由此也和瀑布模型一样地受到 ...

  5. JavaScript(class0526)

    什么是JavaScript? HTML只是描述网页长相的标记语言,没有计算.判断能力,如果所有计算.判断(比如判断文本框是否为空.判断两次密码是否输入一致)都放到服务器端执行的话网页的话页面会非常慢. ...

  6. Microsoft云备份解决方案Azure Backup的常见配置问题

    这篇博客文章有助于解决 Microsoft云备份解决方案(即 Azure Backup)的常见配置问题.客户通常会在安装或注册 Azure Backup时遇到这些问题.以下是有关如何诊断和解决问题的建 ...

  7. HW6.9

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. HDU-4655 Cut Pieces 数学,贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655 先不考虑相临的有影响,那么总数就是n*prod(ai),然后减去每个相邻的对总数的贡献Σ( Mi ...

  9. Mongodb 和 普通数据库 各种属性 和语句 的对应

    SQL to MongoDB Mapping Chart In addition to the charts that follow, you might want to consider the F ...

  10. LinearLayout使用tips

    1.LinearLayout有divider属性,可以用来分割layout里面的各个组件 eg: a | b | c 如果c为gone的话,那么第二条线会消失,如果没用divider而使用View画线 ...