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 ...
随机推荐
- apache开源项目--HBase
HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. HBase是Goog ...
- Mysql加密方式
MySQL数据库的认证密码有两种方式, MySQL 4.1版本之前是MySQL323加密,MySQL 4.1和之后的版本都是MySQLSHA1加密, MySQL数据库中自带Old_Password(s ...
- Kettle简介
ETL和Kettle简介 ETL即数据抽取(Extract).转换(Transform).装载(Load)的过程.它是构建数据仓库的重要环节.数据仓库是面向主题的.集成的.稳定的且随时间不断变 ...
- Linux nc命令详解
NetCat,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本.因为它短小精悍(1.84版本也不过25k,旧版本或缩减版甚至更小).功能实用,被设计为一个简单.可靠的网络工具,可通 ...
- 第一个GTK+程序
在这一章节中,我们将开始编写第一个GTK+程序. 超级简单的例子 我们要“制造”一个超级简单的GTK+程序.就是显示一个空白的窗口. #include <gtk/gtk.h> int ma ...
- SVN server 安装 并创建用户访问
1. 下载svn 官网https://www.visualsvn.com/server/download/ 或者其他网站都行,下载是VisualSVN-Server-3.5.6.msi. 2.安装 ...
- Spring talk简单配置
XMl文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://w ...
- Ubuntu 14.04 设置静态IP
使用Network Manager UI界面中指定 手动时,无法保存. 通过修改配置文件解决来此问题.记录以下. 如果输入过密码后,就会出现在这个目录下面, 以如下chinaNet为例 gaojing ...
- pip 安装python环境及打包
0.安装虚拟环境 pip install virtualenv virtualenv env1 source env1/bin/activate 1. 将包依赖信息保存在requireme ...
- Linode各机房在中国访问速度性能测试
最近因为google的各种被X的原因,想自己弄个VPS玩玩,比来比去都推荐linode. 因为各种性能测试工具都不靠谱,还是自己机器来的直接,虽然笨拙但是真实可信. 从测试结果上看,明显东京机房的速度 ...