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

就是朴素的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. IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示

    当数据量特别大时,简单地以role进行分段,对实际查找的效率提升并不大.就像上一节开头所说,开发者可以根据球员名字的首字母进行分段,且分成26段.由于段数较多,可以使用UITableView的索引机制 ...

  2. Mac 让 iTerm2 记住用户名密码 expect 脚本

    刚刚用iTerm2的时候,总是要一遍遍的敲用户名.密码. 我在想, 能不能像Windows的软件一样,可以直接让软件记住.然后只要点击一下,就直接ssh到远程服务器上面去了. 之后经过搜索,可以用ex ...

  3. cocos2d-x中的尺寸之三

    通过上面两个文章的分析,我们在这个博文里做个总结: CCEGLView::getFrameSize()返回的是窗口相对于屏幕像素的尺寸,这个尺寸,只要窗口没变化,值就不会变化 CCDirector:: ...

  4. hpu校赛--雪人的高度(离散化线段树)

    1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec  内存限制: 128 MB 提交: 81  解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...

  5. 如何在自己的Activity中去控制EditText的焦点

    在进入一个Activity时,如果这个Activity中有EditText,则这个EditText会自动获取焦点,然后就会弹出软键盘,这样给用户体验不是很好.所以一般会通过代码控制让EditText不 ...

  6. android api 中文 (75)—— AdapterView.OnItemClickListener

    前言 本章内容是android.widget.AdapterView.OnItemClickListener,版本为Android 2.3 r1,翻译来自"麦子",欢迎大家与他交流 ...

  7. c++实现精确计时

    //获取比較准确是程序执行时间 #include<iostream> #include<windows.h> using namespace std; int main(voi ...

  8. hdu3416 Marriage Match IV【最短路+最大流】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297581.html   ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...

  9. Request url 各种属性值

    網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc Request.ApplicationPath / Request.Phys ...

  10. (译)Node.js的模块-exports和module.exports

    原文标题:Node.js Module – exports vs module.exports 原文链接:http://www.hacksparrow.com/node-js-exports-vs-m ...