这道题目一点也不卡素数的判断

就是朴素的sqrt(n) 也不卡

所以~放心的用吧。

构造回文的时候看了HINT

其中是这么写的:

Generate palindromes by combining digits properly. You might need more than one of the loops like below.

/* generate five digit palindrome: */
for (d1 = 1; d1 <= 9; d1+=2) { /* only odd; evens aren't so prime */
for (d2 = 0; d2 <= 9; d2++) {
for (d3 = 0; d3 <= 9; d3++) {
palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;
... deal with palindrome ...
}
}
}

 

Source Code:

/*
ID: wushuai2
PROG: pprime
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; bool primei(int n){
int i, j;
for(i = ; i <= sqrt(n); ++i){
if(n % i == ) return false;
}
return true;
} ll b[]={,,};
int p[]={,,,,,,,};
bool prime(int n){
int i = , j, q;
if(n == ) return false;
if(n == || n == || n == ) return true;
if(n % == || n % == || n % == ) return false;
q = (int)sqrt((double)n);
for(; i <= q; ){
for(j = ; j < ; ++j){
if(n % i == ) return false;
i += p[j];
}
if(n % i == ) return false;
}
return true;
}
int creat(){
int i, j, k, l, m, count = ;
for(i = ; i <= ; i += )
for(j = ; j <= ; ++j)
b[count++] = * i + * j + i;
for(i = ; i <= ; i += )
for(j = ; j <= ; ++j)
for(k = ; k <= ; ++k)
b[count++] = * i + * j + k * + j * + i;
for(i = ; i <= ; i += )
for(j = ; j <= ; ++j)
for(k = ; k <= ; ++k)
for(l = ; l <= ; ++l)
b[count++] = * i + * j + k * + l * + k * + j * + i;
return count - ;
} int main() {
ofstream fout ("pprime.out");
ifstream fin ("pprime.in");
int i, j, k, t, n, s, c, w, q;
int a;
n = creat();
fin >> a >> c;
for(i = ; i < n; ++i){
if(b[i] >= a){
if(b[i] > c) break;
if(prime(b[i])){
fout << b[i] << endl;
}
}
} fin.close();
fout.close();
return ;
}

USACO Prime Palindromes 构造回文数的更多相关文章

  1. 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】

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

  2. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  3. [LeetCode] 906. Super Palindromes 超级回文数

    Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...

  4. 4190. Prime Palindromes 一亿以内的质数回文数

    Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...

  5. COGS 678. 双重回文数

    ★   输入文件:dualpal.in   输出文件:dualpal.out   简单对比时间限制:1 s   内存限制:128 MB Dual Palindromes 双重回文数 描述 [USACO ...

  6. POJ2402 Palindrome Numbers 回文数

    题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...

  7. [swustoj 371] 回文数

    回文数(0371) 问题描述 一个自然数如果把所有数字倒过来以后和原来的一样,那么我们称它为回文数.例如151和753357.我们可以把所有回文数从小到大排成一排:1, 2, 3, 4, 5, 6, ...

  8. 洛谷 P1207 [USACO1.2]双重回文数 Dual Palindromes

    P1207 [USACO1.2]双重回文数 Dual Palindromes 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而7777 ...

  9. 洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes

    P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 ...

随机推荐

  1. 【转载】python 模块 - random生成随机数模块

    随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5)) ...

  2. 关于 free() 函数用法的若干疑问

    <C语言参考手册>中关于 free() 函数有如下描述. (1)free() 函数的原型 void free(void *ptr); (2)free 函数对以前由 malloc.callo ...

  3. 批量 GBK 转 UTF8 java

    package encoding; import java.io.File; import java.io.IOException; import java.util.Collection; impo ...

  4. 项目管理软件伙伴https://www.huobanyun.cn/

    现在项目管理软件市面上很多,但能够完全适合每家公司需求的比较难找,因为众口难调,每家公司都有自己的特殊情况,所以,建议考虑下有比较齐全的基础功能的标准化软件产品,同时又在项目管理开发能力上比较突出. ...

  5. QT太多的内容和模块,怎么办?

    我有个问题,QT可以做许多不同的开源项目,而且每个QT新版本都那么内容,感觉学不过来.用不过来那么我们还应该学习和使用其它语言吗? 如果回答,在需要的时候学习,那么这句话意味着,这几年你基本上就局限于 ...

  6. QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NETQT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET

    QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET QT在ui文件上建立信号操机制会不会对后期维护产生影响 分类: 学习心得 2 ...

  7. 使用jodconverter和swftools实现文件在线预览

    参考:仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF 文档在线预览主要用到如下两个工具 1,安装openoffice(同时下载jodcon ...

  8. 禁用Visual Studio 2013的Browser Link功能

    禁用Visual Studio 2013的Browser Link功能 GET http://localhost:37478/7fd25f8af33f443494e765be19be6240/brow ...

  9. C#多线程编程简述

    NET将关于多线程的功能定义在System.Threading名字空间中.因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;). a.启动线程顾名思义,“启动 ...

  10. kali linux

    1. Kali 2.0使用SSH进行远程登录  http://jingyan.baidu.com/article/eae07827a3e6bc1fec5485e3.html 一.配置SSH参数修改ss ...