回文质数 USACO
因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数.
写一个程序来找出范围[a,b](5<=a<b<=100,000,000)间的所有回文质数;
因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数.写一个程序来找出范围[a,b](5<=a<b<=100,000,000)间的所有回文质数;
*Line 1: a,b
a与b之间(含)的所有回文质数
一行一个
5 500
5
7
11
101
131
151
181
191
313
353
373
383
先找回文数,再判断是不是质数。
代碼實現(codevs 38ms,洛谷 44ms):
#include<cstdio>
#include<algorithm>
using namespace std;
int a,b,c,s;
int hw[];
void find(int x){
c=s=x;c/=;
while(c){s*=;s+=c%;c/=;}//构造奇数数位的回文。
if(s>b) return;
if(s>=a) hw[++hw[]]=s;//满足条件的回文,录入。
c=s=x;
while(c){s*=;s+=c%;c/=;}//构造偶数数位的回文。
if(s>b) return;
if(s>=a) hw[++hw[]]=s;//满足条件的回文,录入。
for(int i=;i<;i++) find(*x+i);//继续寻找。
}
int main(){
scanf("%d%d",&a,&b);
for(int i=;i<=;i+=){
s=i;
find(i);
}
sort(hw+,hw+hw[]+);//排序,因为要求顺序输出
for(int i=;i<=hw[];i++)
for(int j=;j*j<=hw[i];){
if(hw[i]%j==) break;
j++;
if(j*j>hw[i]) printf("%d\n",hw[i]);//输出素数。
}
return ;
}
应老师要求,稍微优化了一下(codevs 4ms,洛谷 25ms):
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int a,b,c,s;
int hw[];
int zs[maxn],zsm,d,l,re;
char ch[];
bool v[maxn+];
int read(){
l=,re=;
while(ch[l]=getchar())
if(ch[l]>=''&&ch[l]<='') break;
l++;
while(ch[l]=getchar()){
if(ch[l]>=''&&ch[l]<='') l++;
else break;
}
for(int i=;i<l;i++) re=re*+ch[i]-'';
return re;
}
void write(int x){
l=;
while(x){ch[l++]=x%+'';x/=;}
for(int i=l-;i>=;i--) putchar(ch[i]);
putchar('\n');
}
void find(int x){
c=s=x;c/=;
while(c){s*=;s+=c%;c/=;}
if(s>b) return;
if(s>=a&&(s%||s==)) hw[++hw[]]=s;
for(int i=;i<;i++) find(*x+i);
}
int main(){
for(int i=;i<=maxn;i++)
if(!v[i]){
zs[zsm++]=i;d=i+i;
while(d<=maxn){v[d]=;d+=i;}
}
a=read();b=read();
for(int i=;i<=;i+=){s=i;find(i);}
if(>=a&&<=b) hw[++hw[]]=;
sort(hw+,hw+hw[]+);
for(int i=;i<=hw[];i++){
d=;
while(){
if(hw[i]%zs[d++]==) break;
if(zs[d]*zs[d]>hw[i]||d>zsm){write(hw[i]);break;}
}
}
return ;
}
codevs上跑4ms的不是这段代码,但这段代码理论上更优,可能是评测机不在状态吧。不敢继续攻击网站了。
题目描述
因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。
写一个程序来找出范围[a,b](5 <= a < b <= 100,000,000)( 一亿)间的所有回文质数;
输入输出格式
输入格式:
第 1 行: 二个整数 a 和 b .
输出格式:
输出一个回文质数的列表,一行一个。
输入输出样例
5 500
5
7
11
101
131
151
181
191
313
353
373
383
说明
Hint 1: Generate the palindromes and see if they are prime.
提示 1: 找出所有的回文数再判断它们是不是质数(素数).
Hint 2: Generate palindromes by combining digits properly. You might need more than one of the loops like below.
提示 2: 要产生正确的回文数,你可能需要几个像下面这样的循环。
题目翻译来自NOCOW。
USACO Training Section 1.5
产生长度为5的回文数:
for (d1 = 1; d1 <= 9; d1+=2) { // 只有奇数才会是素数
for (d2 = 0; d2 <= 9; d2++) {
for (d3 = 0; d3 <= 9; d3++) {
palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(处理回文数...)
}
}
}
如果先判素数,再判断是不是回文数会超时。
因为,很明显,素数比回文数多得多。
代码实现(88):
1 #include<cstdio>
2 int a,b,c,d,l;
3 char ch[12];
4 bool v[100000010];
5 int main(){
6 scanf("%d%d",&a,&b);
7 for(int i=2;i<=b;i++)
8 if(!v[i]){
9 c=2*i;d=i;l=0;
10 if(i>=a){
11 for(l=0;l<9;l++){
12 ch[l]=d%10+'0';d/=10;
13 if(!d) break;
14 }
15 for(int i=0;i<=l;i++){
16 if(ch[i]!=ch[l-i]) break;
17 if(i==l) printf("%s\n",ch);
18 }
19 }
20 while(c<=b){v[c]=1;c+=i;}
21 }
22 return 0;
23 }
应该先找到满足要求的回文数,而后判断其是否为素数。
代码实现(AC):
1 #include<cstdio>
2 #include<algorithm>
3 using namespace std;
4 int a,b,c,s;
5 int hw[300000];
6 void find(int x){//find到所有回文数,并存入hw数组。
7 c=s=x;c/=10;
8 while(c){s*=10;s+=c%10;c/=10;}
9 if(s>b) return;
10 if(s>=a) hw[++hw[0]]=s;
11 c=s=x;
12 while(c){s*=10;s+=c%10;c/=10;}
13 if(s>b) return;
14 if(s>=a) hw[++hw[0]]=s;
15 for(int i=0;i<10;i++) find(10*x+i);
16 }
17 int main(){
18 scanf("%d%d",&a,&b);
19 for(int i=1;i<=9;i+=2){
20 s=i;
21 find(i);
22 }
23 sort(hw+1,hw+hw[0]+1);//因为调整回文输入先后太麻烦,所有加了个快排,当然,由于上面的原因,时间上是允许的。
24 for(int i=1;i<=hw[0];i++)
25 for(int j=2;j*j<=hw[i];){
26 if(hw[i]%j==0) break;j++;
27 if(j*j>hw[i]) printf("%d\n",hw[i]);
28 }
29 return 0;
30 }
有点恶心。
题目来源 CODE[VS],洛谷
回文质数 USACO的更多相关文章
- 【USACO 1.5.2】回文质数
[题目描述] 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,0 ...
- 回文质数 Prime Palindromes
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- P1217 [USACO1.5]回文质数 Prime Palindromes
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- 洛谷 P1217 回文质数
洛谷 P1217 回文质数 链接 https://www.luogu.org/problem/P1217 题目 题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 ...
- luogu P1217 [USACO1.5]回文质数 Prime Palindromes x
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(stringstream,sizeof(num)/sizeof(num[0]),打表)
题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围 [a,b](5≤a<b≤100,000,000)( 一亿)间 ...
随机推荐
- Java并发编程系列之CyclicBarrier详解
简介 jdk原文 A synchronization aid that allows a set of threads to all wait for each other to reach a co ...
- [C陷阱和缺陷] 第7章 可移植性缺陷
C语言在许多不同的系统平台上都有实现.的确,使用C语言编写程序的一个首要原因就是,C程序能够方便地在不同的编程环境中移植. 不同的系统有不同的需求,因此我们应该能够预料到,机器不同则其上的C语 ...
- [洛谷3930]SAC E#1 - 一道大水题 Knight
Description 他们经常在一起玩一个游戏,不,不是星际争霸,是国际象棋.毒奶色觉得F91是一只鸡.他在一个n×n的棋盘上用黑色的城堡(车).骑士(马).主教(象).皇后(副).国王(帅).士兵 ...
- ACM_求N^N的前5位数和后5位数(数论)
NNNNN Time Limit: 2000/1000ms (Java/Others) Problem Description: 对于整数N,求N^N的前5位和后5位(1057题加强版) Input: ...
- HTML DOM getElementById() 方法
定义和用法 getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用. 语法 document.getElementById(id) 说明 HTML DOM 定义了多种查找元素 ...
- md5加密、Des加密对称可逆加密、RSA非对称可逆加密、https单边验证、银行U盾双边认证
1.md5不可逆的加密方式,加密成一个32位的字符串.算法是公开的,任何语言的加密结果都是一样的.总有可能是重复的. 用途: (1)防止明文存储:可以用作密码加密 ...
- NodeJS —— 自定义流的实现
概述 常见的自定义流有四种,Readable(可读流).Writable(可写流).Duplex(双工流)和 Transform(转换流),常见的自定义流应用有 HTTP 请求.响应,crypto 加 ...
- Java反射机制实战——字段篇
首先,我们来认识几个类. Class(java.lang.Class) Class对象是一个特殊对象,每一个类都有一个Class对象,用来创建该类的“常规”对象.可以通过对象的getClass()方法 ...
- Linux 压力测试及内存检测
常用的 Stress / Performance 工具 http://benjr.tw/532 Linux中的常用内存问题检测工具 https://blog.csdn.net/jinzhuojun/a ...
- PHP实现写LOG日志的代码
这篇文章给大家介绍的内容是关于PHP实现写LOG日志的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. public function write_log(){ //设置目录时间 ...