【洛谷p1217】回文质数
始终要记得凌云壮flag(真香)
说是个搜索,其实感觉更像是一个暴力;
这个题的难度并不是特别大,因为下面的提示太明显了qwq,(而且之前培训也讲过)首先是构造回文数,构造回文数时,有以下几点优化:
优化1:对于一个回文数,若它的位数是偶数(1551,654456等),除11以外,其余所有的回文数都是11的倍数,所以在构造回文数时,只需要构造奇数位的回文数;
优化2:因为回文数第一位等于最后一位(个位),所以只有当第一位是奇数时才有可能是质数,否则一定会是2的倍数;
优化3:(其实不能算优化啦)对于5---100以内的数,既是回文数又是质数的只有5,7,11,因为我比较废,所以我是写出了三位回文数,五位回文数以及七位回文数这三种情况,然后因为数很多嘛,我们也不能全都枚举了(虽然好像也不会炸,亲测不会炸),反正就是不想多写两个for循环【吐舌】,然后在制造回文数的循环里加了两个判断语句;
构造回文数代码:
/*three*/
for(int i=;i<=;i+=)
for(int j=;j<=;j++){
if(i* + j* + i < a) continue;
hw[++cnt] = i* + j* + i;
if(hw[cnt] > b) {cnt--;break;}
} /*five*/
for (int d1 = ; d1 <= ; d1+=)
for (int d2 = ; d2 <= ; d2++)
for (int d3 = ; d3 <= ; d3++){
if(*d1 + *d2 +*d3 + *d2 + d1 < a) continue;
hw[++cnt] = *d1 + *d2 +*d3 + *d2 + d1;
if(hw[cnt] > b) {cnt--;break;}
} /*seven*/
for(int d1 = ;d1 <= ;d1 += )
for(int d2 = ;d2 <= ;d2++)
for(int d3 = ;d3 <= ;d3++)
for(int d4 = ;d4 <= ;d4++){
if(d1* + d2* + d3* + d4* + d3* + d2* + d1<a) continue;
hw[++cnt] = d1* + d2* + d3* + d4* + d3* + d2* + d1;
if(hw[cnt] > b) {cnt--;break;}
}
接下来在构造出的回文数中判断质数,直接写了一个O(√n)的素数判定就好啦;
ac代码:
#include<bits/stdc++.h> using namespace std; int a,b;
int hw[]={,,},cnt=,str; bool pri(int k){
for(int i=;i<=sqrt(k);i++)
if(k%i==) return ;
return ;
} int main(){
scanf("%d %d",&a,&b);
/*three*/
for(int i=;i<=;i+=)
for(int j=;j<=;j++){
if(i*100 + j*10 + i < a) continue;//
hw[++cnt] = i* + j* + i;
if(hw[cnt] > b) {cnt--;break;}//
} /*five*/
for (int d1 = ; d1 <= ; d1+=){
for (int d2 = ; d2 <= ; d2++){
for (int d3 = ; d3 <= ; d3++){
if(10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1 < a) continue;//
hw[++cnt] = *d1 + *d2 +*d3 + *d2 + d1;
if(hw[cnt] > b) {cnt--;break;}//
}
}
} /*seven*/
for(int d1 = ;d1 <= ;d1 += )
for(int d2 = ;d2 <= ;d2++)
for(int d3 = ;d3 <= ;d3++)
for(int d4 = ;d4 <= ;d4++){
if(d1*1000000 + d2*100000 + d3*10000 + d4*1000 + d3*100 + d2*10 + d1<a) continue;//
hw[++cnt] = d1* + d2* + d3* + d4* + d3* + d2* + d1;
if(hw[cnt] > b) {cnt--;break;}//
} for(int i=;i</*cnt*/;i++)
if(hw[i]<a) str++;
/*for(int i=cnt;i>=str;i--)
if(hw[i]>b) cnt--;*/
for(int i=str;i<=cnt;i++)
if(pri(hw[i])) cout<<hw[i]<<endl;
}//两种不同的写法看习惯吧
end-
【洛谷p1217】回文质数的更多相关文章
- 洛谷 P1217 回文质数
洛谷 P1217 回文质数 链接 https://www.luogu.org/problem/P1217 题目 题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 ...
- 洛谷P1217回文质数-Prime Palindrome回溯
P1217 [USACO1.5]回文质数 Prime Palindromes 题意:给定一个区间,输出其中的回文质数: 学习了洛谷大佬的回溯写法,感觉自己写回溯的能力不是很强: #include &l ...
- 洛谷P1217 回文质数
题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围 [a,b] (5 \le a < b \le 100,000 ...
- 洛谷 - P1217 - 回文质数 - 枚举
https://www.luogu.org/problemnew/show/P1217 考虑暴力生成所有的回文数然后再判断是不是质数.注意个位的选择实际上只有4种.所以是 $4*10^3*10^3=4 ...
- 洛谷 P1015 回文数 Label:续命模拟QAQ
题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...
- 洛谷 P1015 回文数
#include<iostream> #include<cstdio> #include<cmath> #include<string> #includ ...
- 洛谷 P2010 回文日期
Noip2016普及组T2 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月 份,最后2位代表日期 ...
- 洛谷 P2010 回文日期 题解
P2010 回文日期 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用88位数字表示一个日期,其中,前44位代表年份,接下来22位代表月 份,最后22位代表日 ...
- 洛谷 - P3649 - 回文串 - 回文自动机
https://www.luogu.org/problem/P3649 #include <bits/stdc++.h> using namespace std; typedef long ...
- 洛谷P1435 回文子串
题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...
随机推荐
- linux 服务器与客户端异常断开连接问题
服务器与客户端连接,客户端异常断掉之后服务器端口仍然被占用, 到最后是不是服务器端达到最大连接数就没法连接了?领导让我测试这种情况,我用自己的电脑当TCP Client,虚拟机当服务器,连接之后能正常 ...
- powerdesigner数据库设计
(1)创建物理数据模型 打开PowerDesigner,然后点击File-->New Model然后选择如下图所示的物理数据模型(物理数据模型的名字自己起,然后选择自己所使用的数据库即可) ( ...
- RabbitMQ 全套
本博客代码运行环境 ErLang: ErLang_X64_22 version RabbitMQ: RabbitMQ_Server_3.7.15 version python : Python 3.7 ...
- 【转】一文搞懂C语言回调函数
转:https://segmentfault.com/a/1190000008293902?utm_source=tag-newest 什么是回调函数 我们先来看看百度百科是如何定义回调函数的: 回调 ...
- 怎样group by一列 select多列
之前sql用的少 竟然不知道这个小技巧 1 将要查询的列 添加到group by后面(会影响查询结果) 2 使用聚合函数如 max select a.accounttitlecode, max(b.c ...
- Java数组重修,猜数小游戏改进和打印正三角形
数组重修,猜数小游戏 要求:从键盘输入一个数,判断数组是否包含此数,运用随机数 我们可能会这样写 import java.util.Random; import java.util.Scanner; ...
- python的次方操作
好简单,不需要import任何包 b=a**n就是求a的n次方,如果n=0.5就是开方 如果开方的是负数或者附复数,需要 import math b=math.sqrt(a) 这样
- 【leetcode】1034. Coloring A Border
题目如下: Given a 2-dimensional grid of integers, each value in the grid represents the color of the gri ...
- CollectionUtils工具类中常用方法
@SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...
- swift中为什么要创造出可选型?
(1)因为nil这个东西,swift中没有就是没有. Int? 叫 整型可选型,如果不提前声明,直接赋值变量 nil会报错 . 可以将Int赋值给Int? ,但是不能将Int?赋值给Int . ...