USACO Prime Palindromes 构造回文数
这道题目一点也不卡素数的判断
就是朴素的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 构造回文数的更多相关文章
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- [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 ...
- 4190. Prime Palindromes 一亿以内的质数回文数
Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...
- COGS 678. 双重回文数
★ 输入文件:dualpal.in 输出文件:dualpal.out 简单对比时间限制:1 s 内存限制:128 MB Dual Palindromes 双重回文数 描述 [USACO ...
- POJ2402 Palindrome Numbers 回文数
题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...
- [swustoj 371] 回文数
回文数(0371) 问题描述 一个自然数如果把所有数字倒过来以后和原来的一样,那么我们称它为回文数.例如151和753357.我们可以把所有回文数从小到大排成一排:1, 2, 3, 4, 5, 6, ...
- 洛谷 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 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 ...
随机推荐
- FOREIGN KEY相关
在添加外键的时候可以在最后通过ON指定行为和三个参数,来表示操作主表数据之后外表的变化 比如若是删除主表之后的变化,就可以 ON DELETE + 三个参数 --删除department表中相关数据行 ...
- springmvc附件上传核心代码
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.Comm ...
- 二、Python-----用户交互
1.用户交互 Python 3.0的写法 name = input("Please input your name:") Python 2.0的写法 name = raw_inpu ...
- python笔记之第一次使用(未入门)
刚开始学习python,之前完全没有接触,在这里写下自己学习python的过程和心得. 首先,安装python,打开官网:http://python.org.点击Download.我选择了3.5.1版 ...
- 原型扩展的方法解决IE和Firefox的Js兼容问题
if(!document.all){//textContent->text Element.prototype.__defineGetter__('text',function(){ret ...
- perl unicode utf-8 \x转换
[root@dr-mysql01 ~]# cat x5.pl use Encode; use JSON; use URI::Escape; use LWP::Simple; my $host = &q ...
- DBCC CHECKDB用法 手工修复数据库
快速修复 DBCC CHECKDB ('数据库名', REPAIR_FAST) 重建索引并修复 DBCC CHECKDB ('数据库名', REPAIR_REBUILD) 如果必 ...
- struts2自己定义类型转换器
1.1. struts2自己定义类型转换器 1) 自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...
- poj2125Destroying The Graph(最小割+输出方案)
题目请戳这里 题目大意:给一张有向图,现在要选择一些点,删掉图中的所有边.具体操作为:选择点i,可以选择删除从i出发的所有有向边或者进入i的所有有向边,分别有个代价ini和outi,求最小的代价删掉所 ...
- 通过Manifest的配置信息实现页面跳转,及总结
1:新建一个xml文件,如second_view.xml文件,然后新建一个Activity如SecondActivity.java并在里面设置setContentView(R.layout.secon ...