USACO 1.5 Prime Palindromes
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 .
PROGRAM NAME: pprime
INPUT FORMAT
Line 1: | Two integers, a and b |
SAMPLE INPUT (file pprime.in)
- 5 500
OUTPUT FORMAT
The list of palindromic primes in numerical order, one per line.
SAMPLE OUTPUT (file pprime.out)
- 5
- 7
- 11
- 101
- 131
- 151
- 181
- 191
- 313
- 353
- 373
- 383
- ——————————————————————题解
就是枚举回文数然后筛,写了一个logn的筛法(Miller Rabin),结果乘法没开longlong导致wa了一次
枚举素数我手敲了八个循环……后来ac看analysis……只要10-9999然后翻转就行orz我感觉自己真是个辣鸡
而且这位小哥以神一般的数学直觉便判断出任何二倍长的回文数一定是11的倍数,那么我们就不用考虑偶数个的了。小学数学老师你还要我吗要我吗……
这轻描淡写的一句话顶上了我30++行代码orz
那个版权不敢盗,所以不粘别人代码,贴上我易读的代码……以及写的很丑的Miller Rabin(check函数)
- /*
- PROB: pprime
- LANG: C++
- ID: jiaqi si
- */
- #include <iostream>
- #include <string.h>
- #include <cstdlib>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #include <ctime>
- #define ivory
- #define mo 1000000007
- #define siji(i,x,y) for(int i=(x);i<=(y);i++)
- #define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
- #define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
- #define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
- #define pii pair<int,int>
- #define fi first
- #define se second
- #define mo 1000000007
- using namespace std;
- int ans[],cnt;
- int a,b;
- int mpow(int c,int d,int k) {
- if(d==) {
- return c%k;
- }
- else {
- int tmp=mpow(c,d/,k)%k;
- if(d&) {
- return 1LL*tmp*tmp%k*c%k;
- }
- else {
- return 1LL*tmp*tmp%k;
- }
- }
- }
- bool _check(int v,int s,int k) {
- if(v==) return true;
- siji(i,,s) {
- if(1LL*v*v%k==) {
- if(v%k==k-){return true;}
- else return false;
- }
- v=1LL*v*v%k;
- }
- return false;
- }
- bool check(int k) {
- int tmp=k-;
- int s=;
- while(tmp%(<<s)==) ++s;
- --s;
- int d=tmp/(<<s);
- int tmp1=mpow(,d,k);
- int tmp2=mpow(,d,k);
- int tmp3=mpow(,d,k);
- bool f=;
- if(!_check(tmp1,s,k)) {f=;}
- else if(k> && (!_check(tmp2,s,k))) {f=;}
- else if(k> && (!_check(tmp3,s,k))) {f=;}
- return f;
- }
- int binary(int k){
- int l=,r=cnt;
- while(l<r) {
- int mid=(l+r+)>>;
- if(ans[mid]>=k) r=mid-;
- else l=mid;
- }
- return l;
- }
- int main() {
- #ifdef ivory
- freopen("pprime.in","r",stdin);
- freopen("pprime.out","w",stdout);
- #else
- //freopen("f1.in","r",stdin);
- #endif
- ans[++cnt]=;
- siji(i,,) {
- if(i%!= && check(i)) ans[++cnt]=i;
- }
- siji(i,,) {
- if(i%==)continue;
- int tmp=i*+i;
- if(check(tmp)) ans[++cnt]=tmp;
- }
- siji(i,,) {
- if(i%==) continue;
- siji(j,,) {
- int tmp=i*+j*+i;
- if(check(tmp)) ans[++cnt]=tmp;
- }
- }
- siji(i,,) {
- if(i%==) continue;
- siji(j,,) {
- int tmp=i*+j*+j*+i;
- if(check(tmp)) ans[++cnt]=tmp;
- }
- }
- siji(i,,) {
- if(i%==) continue;
- siji(j,,) {
- siji(k,,) {
- int tmp=i*+j*+k*+j*+i;
- if(check(tmp)) ans[++cnt]=tmp;
- }
- }
- }
- siji(i,,) {
- if(i%==) continue;
- siji(j,,) {
- siji(k,,) {
- int tmp=i*+j*+k*+k*+j*+i;
- if(check(tmp)) ans[++cnt]=tmp;
- }
- }
- }
- siji(i,,) {
- if(i%==) continue;
- siji(j,,) {
- siji(k,,) {
- siji(h,,) {
- int tmp=i*+j*+k*+h*+k*+j*+i;
- if(check(tmp)) ans[++cnt]=tmp;
- }
- }
- }
- }
- siji(i,,) {
- if(i%==) continue;
- siji(j,,) {
- siji(k,,) {
- siji(h,,) {
- int tmp=i*+j*+k*+h*+h*+k*+j*+i;
- if(check(tmp)) ans[++cnt]=tmp;
- }
- }
- }
- }
- scanf("%d%d",&a,&b);
- int il=binary(a);
- int ir=binary(b);
- if(ans[ir+]==b) ir++;
- siji(i,il+,ir) {
- printf("%d\n",ans[i]);
- }
- }
USACO 1.5 Prime Palindromes的更多相关文章
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- luogu P1217 [USACO1.5]回文质数 Prime Palindromes x
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- 4190. Prime Palindromes 一亿以内的质数回文数
Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...
- <Sicily>Prime Palindromes
一.题目描述 The number 151 is a prime palindrome because it is both a prime number and a palindrome (it i ...
- USACO Prime Palindromes 构造回文数
这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...
- 【USACO 1.5】Prime Palindromes
/* TASK: pprime LANG: C++ SOLVE: 枚举数的长度,dfs出对称的数,判断是否在范围内,是否是素数 原来想着枚举每个范围里的数,但是显然超时,范围最大是10^9. 对称的数 ...
- USACO Section 1.5 Prime Palindromes 解题报告
题目 题目描述 题目就是给定一个区间[a,b]((5 <= a < b <= 100,000,000)),我们需要找到这个区间内所有既是回文串又是素数的数字. 输入样例 5 500 ...
随机推荐
- webStorm支持.wxml文件高亮显示
微信小程序官方说明需要在微信开发者工具中开发运行,但这个工具着实不咋地. 我是使用webstrom编辑,然后在微信开发者工具中热加载查看效果,因为webstrom默认并不支持*.wxml,添加使用xm ...
- public static void speckOnWin7(string text),在win7中读文字
public static void speckOnWin7(string text) { //洪丰写的,转载请注明 try { string lsSource = ""; ...
- http缓存与cdn相关技术
阅读目录 一 http缓存 二.Http缓存概念解析 三.cdn相关技术 摘要:最近要做这个主题的组内分享,所以准备了一个星期,查了比较多的资料.准备的过程虽然很烦很耗时间,不过因为需要查很多的资料, ...
- 分离JavaScript
分离JavaScript类似于使用style属性,在HTML文档里使用诸如onclick之类的属性也是一种既没有效率又容易引发问题的做法.如果我们用一个"挂钩",就像CSS机制中的 ...
- CG 标准函数库
(1)数学函数 函数 功能描述 abs(x) 返回输入参数的绝对值 acos(x) 反余切函数,输入参数范围为[-1,1], 返回[0,π]区间的角度值 all(x) 如果输入参数均不为0,则返回tu ...
- ssh中的 Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
这个错误我整理了 半天才发现问题的存在. 尝试了网上的很多办法,但是最后都没有达到效果. 包括这两种: 第一种: web.xml种的配置 <filter> <filter-name ...
- log4j基本使用方法
通过配置文件可知,我们需要配置3个方面的内容: 1.根目录(级别和目的地) 2.目的地(控制台和文件等) 3.输出样式 Log4j由三个重要的组件构成: 1.日志信息的优先级 日志信息的优先级从高到低 ...
- python第五天
反射 hasattr,getattr class Foo: static_name = 'nba' def __init__(self): self.name = 'alex' def show(se ...
- iOS SDWebImage的使用
现在把代码贴出来,供大家参考.尤其是新手,看完这篇博客,图片缓存so easy.最后有demo供大家下载,先学习. 第一步,下载SDWebImage,导入工程.github托管地址https://gi ...
- java中的反编译
使用JD-GUI工具 支持mac os 和 windows 地址为:http://jd.benow.ca