4190. Prime Palindromes 一亿以内的质数回文数
The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 100,000,000); both a and b are considered to be within the range .
There are multiple test cases.
Each case contains two integers, a and b.
a=b=0 indicates the end of input.
For each test case, output the list of palindromic primes in numerical order, one per line.
5 500
0 0
5
7
11
101
131
151
181
191
313
353
373
383 题目链接:4190. Prime Palindromes
题意分析:求给定区间内的质数回文数
题目分析:
1.多组测试数据,所以先打表。
2.先求质数再判断回文,效率低下;所以先构造回文数,再判断质数。
3.偶数位的回文数都能被11整除,自己证明去。所以,偶数位的回文数除了11都是合数。
4.一个k位数,可以构造出一个奇数位的回文数。比如13,可以构造131;189可以构造18981.所以100000000内的只要从1构造到9999即可。
5.若范围为1000000000,那么明显超出int范围,要用long long。当然此题无此陷阱。
//http://soj.me/show_problem.php?pid=4190
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std; vector<int> v; bool prime(int x)
{
for (int i = ; i < sqrt(x+0.5); i++)
if (x%i == )
return ; return ;
} void get()
{
// 11是唯一的偶数位的质数回文数。
v.push_back(); //构造奇数位的回文数
for (int i = ; i <= ; ++i)
{
int tmp = i/, sum; for (sum = i; tmp != ; tmp /= )
{
sum = sum* + tmp%;
} if (prime(sum))
v.push_back(sum);
}
}
int main()
{
get();
sort(v.begin(), v.end()); //因为是不按顺序push,所以要sort
int a, b;
while(cin >> a >> b, a||b)
{
for (int i = ; i < v.size(); ++i)
{
if (v[i] >= a)
{
if (v[i] > b)
break; printf("%d\n", v[i]);
}
}
}
}
4190. Prime Palindromes 一亿以内的质数回文数的更多相关文章
- P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- 洛谷 P1207 [USACO1.2]双重回文数 Dual Palindromes
P1207 [USACO1.2]双重回文数 Dual Palindromes 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而7777 ...
- 洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes
P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 ...
- USACO Prime Palindromes 构造回文数
这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...
- URAL 2040 Palindromes and Super Abilities 2 (回文自动机)
Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...
- Queries for Number of Palindromes(求任意子列的回文数)
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
- luogu1207双重回文数[usaco1.2]Dual Palindromes
题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上 ...
- [Swift]LeetCode906. 超级回文数 | Super Palindromes
Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...
随机推荐
- 微信小程序 (node) warning: possible EventEmitter memory leak detected
小程序 (node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setM ...
- 如何设置outlook实现自动秘密抄送邮件的功能?
很多朋友会发现虽然在家里同步了公司的邮箱可以正常收发邮件,可是每当使用家里的outlook发送相关邮件的时候,在公司的邮箱里找不到相关的发件记录,只能同步收件箱,而不能同步发件箱应该是比较让人困扰的问 ...
- marquee.js
图片无缝滚动工具类 CreateTime--2018年3月7日17:11:03 Author:Marydon /** * 图片无缝滚动 * @description * 将要滚动的图片复制一份作为 ...
- (二)Solr——Solr界面介绍
1. Dashboard 仪表盘,显示了该Solr实例开始启动运行的时间.版本.系统资源.jvm等信息. 2. Logging Solr运行日志信息 3. Cloud Cloud即SolrCloud, ...
- PHP-php-fpm占用系统资源分析
1.别的先不管,先top看一下cpu.ram.swap哪个比较紧张. 由上图分析,可以看出共有602个进程,其中有601个进程休眠了.这好像有点不对劲,内核进程也就80个左右,加上memcached, ...
- Qt-优化布局结构
在迄今为止讲到每一个例子中,我们只是简单的把窗口部件放置到某个确定的布局中.但在某些情况下,由此形成的布局看起来可能还不是我们最想要的形式.在这些情形中,可以通过改变要摆放的窗口部件的大小策略和大小提 ...
- js 复制内容到粘贴板的兼容性
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...
- Nodejs 命令行交互神奇 yargs
传送门: # example https://github.com/yargs/yargs/blob/master/docs/examples.md # 官网 http://yargs.js.org/ ...
- MySQL-group-replication 配置
MySQL-Group-Replication 是mysql-5.7.17版本开发出来的新特性:它在master-slave 之间实现了强一致性, 但是就目前来说主要是性能不太好. [1]确定当前的m ...
- BASE64 编码解码
/// <summary> /// Base64编码 /// </summary> /// <param name="data"></pa ...