回文质数【题目链接】

始终要记得凌云壮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】回文质数的更多相关文章

  1. 洛谷 P1217 回文质数

    洛谷 P1217 回文质数 链接 https://www.luogu.org/problem/P1217 题目 题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 ...

  2. 洛谷P1217回文质数-Prime Palindrome回溯

    P1217 [USACO1.5]回文质数 Prime Palindromes 题意:给定一个区间,输出其中的回文质数: 学习了洛谷大佬的回溯写法,感觉自己写回溯的能力不是很强: #include &l ...

  3. 洛谷P1217 回文质数

    题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围 [a,b] (5 \le a < b \le 100,000 ...

  4. 洛谷 - P1217 - 回文质数 - 枚举

    https://www.luogu.org/problemnew/show/P1217 考虑暴力生成所有的回文数然后再判断是不是质数.注意个位的选择实际上只有4种.所以是 $4*10^3*10^3=4 ...

  5. 洛谷 P1015 回文数 Label:续命模拟QAQ

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  6. 洛谷 P1015 回文数

    #include<iostream> #include<cstdio> #include<cmath> #include<string> #includ ...

  7. 洛谷 P2010 回文日期

    Noip2016普及组T2 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月 份,最后2位代表日期 ...

  8. 洛谷 P2010 回文日期 题解

    P2010 回文日期 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用88位数字表示一个日期,其中,前44位代表年份,接下来22位代表月 份,最后22位代表日 ...

  9. 洛谷 - P3649 - 回文串 - 回文自动机

    https://www.luogu.org/problem/P3649 #include <bits/stdc++.h> using namespace std; typedef long ...

  10. 洛谷P1435 回文子串

    题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...

随机推荐

  1. Docker实战部署应用——Tomcat

    Tomcat 部署 拉取tomcat镜像 docker pull tomcat:8 创建tomcat容器 创建tomcat容器用于 Web应用,并且进行目录映射 docker run -id --na ...

  2. 一、VS支持Vue语法

    一.VS支持Vue语法

  3. image按钮新增的width属性和height属性

    代码实例: test.html <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  4. Mybatis(三)MyBatis 注解方式的基本 用法

    在 MyBatis注解 SQL 中,最基本的就是@Select.@Insert.@Update 和@Delete 四种.

  5. bui拍照上传、相册上传注意事项

    1.控制台输入 bui.currentPlatform  可查看工程项目基于什么平台  如:bingotouch 2.如果是 bingotouch , 在 index.js 或者其它配置的地方, 加上 ...

  6. Python3解leetcode Subtree of Another Tree

    问题描述: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  7. CSS中属性百分比的基准点

    1.属性百分比的基准点 1.1.基于包含块 以下的关于包含块(含块)的概念,不能简单地理解成是父元素. 如果是静态定位和相对定位,包含块一般就是其父元素.但是对于绝对定位的元素,包含块应该是离它最近的 ...

  8. CF576D Flights for Regular Customers 矩阵乘法 + Bitset优化

    %%%cxhscst2's blog Codeforces 576D Flights for Regular Customers(矩阵加速DP) 代码非常优美 + 简洁,学习到了 Code: #inc ...

  9. 【2019 Multi-University Training Contest 10】

    01: 02: 03:https://www.cnblogs.com/myx12345/p/11671692.html 04: 05:https://www.cnblogs.com/myx12345/ ...

  10. Solr基本命令

    启动Solr 安装Solr后,进入到Solr主目录中的bin文件夹,并使用以下命令启动Solr. [Hadoop@localhost ~]$ cd [Hadoop@localhost ~]$ cd S ...