【洛谷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 ...
随机推荐
- 1. AtomicInteger 、Unsafe 及 CAS方法的整理
本文摘自: https://blog.csdn.net/fanrenxiang/article/details/80623884 http://ifeve.com/sun-misc-unsafe/ h ...
- 后台PDF返回Base64,前台接收预览
读取已存在的PDF文件,path为绝对路径 string base64String = "";byte[] buffer=null; using (FileStream fs = ...
- C# 列出并删除一个文件夹下的所有MD5值相同的文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Taro -- 微信小程序密码弹窗
记录一个类似支付密码的弹窗写法,实现是否免密功能.如图: index.js import Taro, { Component } from '@tarojs/taro' import { Vi ...
- 三、Windows下用FFmpeg+nginx+rtmp搭建直播环境 实现推流、拉流
一.环境 1.开发环境:windows 2.开发工具:FFmpeg.nginx.nginx-rmtp-module (链接:https://pan.baidu.com/s/119d2GeMzddas_ ...
- linux系统升级openssh
一.升级前准备工作 安装openssh过程需gcc,zlib-devel,openssl-devel,等编译环境,如果通过rpm包来安装,需要解决各种依赖包,故配置本地yum源解决依赖问题. 1. 配 ...
- frugally-deep: Header-only library for using Keras models in C++
// Convenience wrapper around predict for models with // single tensor outputs of shape (1, 1, 1), / ...
- MyCAT操作MySQL示例之E-R表
接着上一篇继续..... E-R 关系的数据分片策略,子表的记录与所关联的父表记录存放在同一个数据分片上,即子表依赖于父表,通过表分组(Table Group)保证数据 Join 不会跨库操作. 表分 ...
- Integrating .NET Code and SQL Server Reporting Services
SQL Server Reporting Services versions 2000 and 2005 (SSRS) has many powerful features. SSRS has a w ...
- spring+hibernate单元测试案例
1,maven创建web工程 2,导入相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...