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 ...
随机推荐
- shell编程——if语句 if -z -n -f -eq -ne -lt
if 条件then Commandelse Commandfi 别忘了这个结尾 If语句忘了结尾fitest.sh: line 14: sy ...
- java的四舍五入算法
粗力度的四舍五入为整数 package math; public class MathRoundTest { /** * Math类中提供了三个与取整有关的方法:ceil,floor,round, * ...
- NopCommerce架构分析之参考资料
http://www.cnblogs.com/RobbinHan/archive/2011/11/30/2269537.html 依赖注入框架Autofac的简单使用 http://www.cnblo ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6.1
Given a basis $U=(u_1,\cdots,u_n)$ not necessarily orthonormal, in $\scrH$, how would you compute th ...
- 让你的.NET程序支持多语言
辛辛苦苦做出来的软件,我们当然希望能让更多的人用,支持多语言是必须的.下面我将以Asp.net Web Form为例来介绍如何支持多语言.其他程序比如windows程序,过程都是大同小异的. 大概分以 ...
- setBackgroundMusicLoop for cocos2dx
给cocos2dx的SimpleAudioEngine加上设置背景音乐循环的方法,详见gist.
- Tasks on 2013
1.改进并补充实验 2.样本选取和文档分布调查 3. Diversity Metrics 4. PPT for Project starting 1. Chrome & webkit 2. O ...
- OWC控件的使用
环境:SQL Server 2005 Analysis Services + OWC 1.1 +Win 7 准备: 1.在SQL Server 2005 Analysis Services建好ola ...
- 说说通信设置方式之hostonly
Vmnetcfg.exe是配置它的网络. 虚拟机,虚拟出来有两个.VMnet1和VMnet8. ************************若用hostonly,则需要将VMnet1给启用.*** ...
- windows环境下mysql忘记密码如何重置
本文主要是针对mysql重置密码过程中出现“mysqld不是内部命令或外部命令”的问题而写的.网上有很多关于mysql忘记密码了如何找回的文章,但是很多说的都不够详细,特别是还要用到DOS命令,可能这 ...