PAT 1015
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的更多相关文章
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PAT 1015 德才论 (25)(代码+思路)
1015 德才论 (25)(25 分)提问 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子, ...
- PAT——1015. 德才论
宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人 ...
- pat 1015 Reversible Primes(20 分)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT 1015. 德才论 (25) JAVA
宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子 ...
- PAT 1015. 德才论 (25)
宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子 ...
- PAT 1015 德才论
https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312 宋代史学家司马光在<资治通鉴>中 ...
- PAT 1015 Reversible Primes (判断素数)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
随机推荐
- SGU 438 The Glorious Karlutka River =) ★(动态+分层网络流)
[题意]有一条东西向流淌的河,宽为W,河中有N块石头,每块石头的坐标(Xi, Yi)和最大承受人数Ci已知.现在有M个游客在河的南岸,他们想穿越这条河流,但是每个人每次最远只能跳D米,每跳一次耗时1秒 ...
- Linux串口c_cc[VTIME]和c_cc[VMIN]属性设置的作用
Linux串口c_cc[VTIME]和c_cc[VMIN]属性设置的作用 在串口编程模式下,open未设置O_NONBLOCK或O_NDELAY的情况下. c_cc[VTIME]和c_cc[VMIN] ...
- NGINX(七)分段下载
前言 nginx分段下载通过ngx_http_range_filter_module模块进行处理,关于HTTP分段下载过程,可以参考HTTP分段下载一文,主要分为一次请求一段和一次请求多段 涉及数据结 ...
- 转载--PHP json_encode() 和json_decode()函数介绍
转自:http://www.nowamagic.net/php/php_FunctionJsonEncode.php 转自:http://www.jb51.net/article/30489.htm ...
- [2015更新]用Word2007写CSDN博客
搞了半天终于可以用word2007发布CSDN博客了,特分享出来,以方便其他用户. 所示的界面. 图1 office按钮 所示的管理账号,然后点击"新建"也可以进入图3所示 ...
- java@ LinkedList 学习
package abc.com; import java.util.LinkedList; public class TestLinkedList { static void prt(Object o ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- 扯扯淡,写个更快的memcpy
写代码有时候和笃信宗教一样,一旦信仰崩溃,是最难受的事情.早年我读过云风的一篇<VC 对 memcpy 的优化>,以及<Efficiency geek 2: copying data ...
- rsync 的安装
Server setup 0)yum -y install xinetd vi /etc/xinetd.d/rsync and ensure following: disable = n ...
- SpringMVC日期参数自动绑定
Controller: @RequestMapping("/addUser") public String addUser(User user) { ... } Model: pu ...